Bash Learning Materials

Posted on

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

How to add a prefix string at the beginning of each line in Bash shell script on Linux?

Posted on

How 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 on

How 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 on

How 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 on

Getting 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

Floating Point in Bash Shell

Posted on

Integers 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 on

This 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