General Bash Usage — 1
In this article, I will talk about the concept of Linux Bash. What is Bash? What is Bash Expansion? It’s a good list of Linux Variables , Linux Alias , Linux Pipe Operations... It goes on and on.
Bash is one of the most important and most used shells developed for UNIX. It is a shell or command language interpreter for the GNU operating system. Derived from the initials of the words Bourne Again Shell.
Shell Variables
Since Bash is also an interpreted programming language, basic programming constructs (Variables, Loops, etc.) are also available. A variable is determined by simply naming it and its type is not found.
E.g :
variable = “Cemre”
echo $variableCemre
Alias
We can define our own commands or shortcuts using aliases. Before creating a shortcut, let’s query if an alias is used with the name we want.
type myVar
If there is no definition, let’s create it with the alias command.
E.g :
alias myVar = ‘cd ~ ‘
When we give the myVar command with this command, it will send us to the home directory. To remove this alias also
unalias myVar
We can give the command.
Pipe Operations
Pipe is used to send the output of any command to another command that can process that output. Pipe is used with the ( | ) character. This character sends the output of the commands that come before it to the command that comes after it.
E.g :
Let’s list all the files in any directory in detail and send it to the lpr command, which allows us to print it from our printer.
ls -l | lpr
Here, all the information that the ls -l command will print on our screen has been extracted from our printer with the lpr command.
Now, let’s mention another topic…
In Pipe operations, we sent the output of the command to another command. Here, the purpose is to direct the command we give to a file. If we use the list command again, let’s assume that we have a text file named files.txt. We direct our listed files to this text file as follows.
E.g :
ls -l > files.txt
this command has written the output of our listing command to our files.txt file.
cat files.txt
We can read quickly with the command.
What if I gave a wrong command? Will I be able to see this error in the file? I need to give a different command for this.
E.g :
Let’s try to list a file that is not in our directory.
ls /wrong 2> files.txt
The different “2>” in this command allowed us to suppress the standard error to our files.txt file.
If we make this work a little more colorful, let’s want it to be written in error.txt if the command we give will produce an incorrect output, and in the result.txt files if it will produce a standard output. Let’s use the ls command in our directory and see that it prints the result to result.txt. For this, it will be enough to run the following command.
ls -l 2>error.txt > result.txt
We see that the output is written to result.txt. Well, let’s try to do an incorrect operation in the same command.
ls /non-folder 2>error.txt >result.txt
Now we can see the standard error written to error.txt. There is a small point we should mention here. When using the > sign when redirecting Standard output or Standard error, if you do not have a file with that name, it will be created and the output will be written to the file. If the file exists, its contents are destroyed and the output of the command is written as the new content of the file. In short, your existing information will be lost. If you want to add the output of a new command on top of the existing file, you should use the >> signs.
E.g :
~ text = “Hello Bash”
~ echo $text >> result.txt
Now, when we view our result.txt file with the cat command, you will see that the text “Hello Bash” has been added.
Processes
UNIX is a multitasking operating system. Processes are a running program. Each process can run in the background or under the control of the shell and has its own Process ID (PID). When we run a command in the shell, the Parent Process creates a Child Process. Here, fork() and wait() system calls, which are the main topics of Linux Operating Systems, are used. I won’t go too far on this theoretical part, so as not to confuse it.
We can use the ps command to view these processes in the shell. When this command is used, PID, TTY, TIME and CMD fields appear.
PID, Process ID ,
TIME, CPU time consumed,
TTY, terminal where the process is run,
CMD stands for the name of the program entered from the command line.
Background Process
I have said before that every process runs in the background or under the control of the shell. In order to run a command in the background, we put an “&” sign at the end of the command we give. After giving the command with this character, we will continue from the shell screen again.
E.g :
sleep 100 &
When we issue the command, the shell we are in goes to sleep for 100 seconds, but we continue our operations in another shell. If we give the ps command here again, we will see two processes running, and the process we put to sleep will appear under the CMD with the text sleep 100. It will automatically expire after 100 seconds.
Well, let’s say we give the sleep command in the existing shell. I entered 1000 by mistake, I will be on hold for a very long time and I don’t want to wait for it. For this, I need to interrupt or terminate the program.