In the last chapter, you learned about using more
, less
, head
, and tail
commands, and text processing tools like diff, cut, paste, comm, and uniq. You learned what standard input, output, and standard error are. You also learned about metacharacters and pattern matching using vi and grep.
In this chapter, you will cover the following topics:
When we log in, the
$
sign will be visible in the shell terminal (#
prompt if logged in as root or administrator). The Bash shell runs scripts as interpreter. Whenever we type a command, the BASH shell will read them as series of words (tokens). Each word is separated by a space (
), semi colon (;
), or any other command delimiter. We terminate the command by pressing the Enter key. This will insert a new line character at the end of the command. The first word is taken as a command, then consecutive words are treated as options or parameters.
The shell processes the command line as follows:
$name
and $user
) is performedecho
`cal`
and echo
`date`
) is performedrm *
)The sequence of execution of different types of commands will be as follows:
l
, ll
, egrep
, and similar)for
, if
, while
, and similar)bg
, fg
, source
, cd
, and similar)bin
and sbin
folder)Whenever a command is given in a shell or terminal, the complete command will be tokenized, and then shell will check if the command is alias.
Aliases, keywords, functions, and built-in commands are executed in the current shell, therefore their execution is fast as compared to executable external commands or scripts. Executable external commands will have a corresponding binary file or Shell script file in the file system, which will be stored in any folder. The shell will search the binary file or script of a command by searching in the PATH
environment variable. If we want to know what the type of command it is, such as if it is an alias or a function or internal command, it can be found out by the type
built-in command, which is shown as follows:
$ type mkdir mkdir is /bin/mkdir $ type cd cd is a shell builtin $ type ll ll is aliased to `ls -alF' $ type hello hello is a function hello () { echo "Hello World !"; } $ type for for is a shell keyword
Bash has provision of a few built-in commands to change the sequence of command line processing. We can use these built-in commands to change default behavior of command-line processing.
$ command ls
This will make aliases and functions be ignored and the external ls
command will execute.
builtin
command will work as follows:$ builtin BUILT-IN
This will ignore aliases and functions from the shell environment and only built-in commands and external commands will be processed.
break
built-in command will work as follows:$ builtin –n break
This will make the break built-in to be disabled and the external command break
will be processed.
$ enable
. |
command |
eval |
history |
pwd |
test |
.. |
compgen |
exec |
jobs |
read |
times |
[ |
complete |
exit |
kill |
readarray |
trap |
alias |
compopt |
export |
let |
readonly |
true |
bg |
continue |
false |
local |
return |
type |
bind |
declare |
fc |
logout |
set, unset |
typeset |
break |
dirs |
fg |
mapfile |
shift |
ulimit |
builtin |
disown |
getopts |
popd |
shopt |
umask |
caller |
echo |
hash |
printf |
source |
unalias |
cd |
enable |
help |
pushd |
suspend |
wait |
$ enable –n built-in-command
For example: $ enable –n test
In this case, in my shell, if we have to test an external command, then instead of the internal test
command, the external test
command will be executed.
In Shell scripting, we need to check if the last command has successfully executed or not. For example, whether a file or directory is present or not. As per the result, our Shell script will continue processing.
For this purpose, the BASH shell has one status variable ?
. The status of the last command execution is stored in ?
. The range of numerical value stored in ?
will be from 0 to 255. If successful in execution, then the value will be 0; otherwise, it will be non-zero, which is as follows:
$ ls $ echo $? 0
Here, zero as the return value indicates success.
In the next case, we see:
$ ls /root $ echo $? 2
Here, non-zero value indicates an error in the last command execution.
In the next case, we see:
$ find / -name hello.c $ echo $?
The return value will indicate if the hello.c
file is present or not!