Bash (GNU Bourne-Again SHell) the default shell for many Linux distributions. It is very common for scripting languages in Linux. Bash is easy and straightforward for writing small tools. However, as most tools, it has its grammars that could easily cause bugs if they are not used correctly. Here I summarize a list of good
Read more
Tag: Bash shell
How to search history commands very effectively in bash shell command line?
Posted onHow to search history commands very effectively in bash shell command line? Just enter CTRL+r The function of above is: (reverse-i-search)`’: It is very fast and efficient.
How to add a prefix string at the beginning of each line in Bash shell script on Linux?
Posted onHow to add a prefix string at the beginning of each line in Bash shell script on Linux? For example, assume we have a file a.txt: line 1 line 2 I want to have, pre line 1 pre line 2 You may use sed, the stream editor for filtering and transforming text: sed -e ‘s/^/pre
Read more
How to get the running process’ parent process’ ID in C / C++?
Posted onHow to get the running process’ parent process’ ID in C / C++? In C and C++, you can call the getppid() library function which is a function from the POSIX library. #include <sys/types.h> #include <unistd.h> pid_t getppid(void); getppid() returns the process ID of the parent of the calling process. Example usage: getppid.c #include <stdio.h>
Read more
How to delete or get the number in the tail from a string in shell script?
Posted onHow to delete or get the number in the tail from a string in shell script (bash script)? Okay to call other tools. For example, from “/dev/sda8”, I want to get “/dev/sda” and “8” separately. This can be achieved by using sed as follows. To get the string after deleting the tailing numbers, we can
Read more
Getting Hostname in Bash in Linux in 3 Ways
Posted onGetting the hostname of the node running a program can be useful in various scenarios, such as creating logs with the hostname, identifying which node a script is running on, or configuring a distributed system with different nodes. In Bash, there are several ways to retrieve the hostname of the machine, as mentioned in the
Read more
How to set an environment variable in bash?
Posted onHow to set an environment variable in bash? The syntax in bash for setting an environment variable is as follows. export VARIABLE=value Note that there is no space among the variable, the equals sign (“=”) and the value. If the value has spaces, the value should be put in quotes. To check it: echo $VARIABLE
Read more
Floating Point in Bash Shell
Posted onIntegers are natively supported in Bash shell. However, what if we use floating point in Bash shell? The short and direct answer is using ‘bc‘ command – “An arbitrary precision calculator language.” Just run bc and enter some floating point calculation expression, such as “1.2+8.2”, bc will give the result. In a script, we certainly
Read more
Online Tutorials for Linux Beginners
Posted onThis post compiles a list of tutorials on the Internet for Linux beginners. Linux beginners usually get headache from the “seemed” complexity of the Linux operation system. But once they get into the gate and get familiar with Linux, they will love it. UNIX Tutorial for Beginners “A beginners guide to the Unix and Linux
Read more