Linux commands and arguments
Welcome to our tutorial Linux commands and arguments. In this tutorial we will discuss about shell expansion, linux commands and arguments.
It is important to know about shell expansion because many commands which you execute on your linux system are processed and most likely shell changes them before executing these commands. Command Line interface or CLI or shell used on most of linux systems is called bash (bourn again shell. Bash inherits features from original shell sh, the c shell csh and the korn shell ksh
To demonstrate features of shell we will be using echo command most of time in this tutorial. This command is very simple and it display input as it receives.
[root@shahid ~]# echo Sunny Sunny [root@shahid ~]# echo today its sunny today its sunny [root@shahid ~]#
Linux arguments
One of main feature of shell is to scan a command line , As oon as you write a command in CLI and hit enter shell will first scan line. It will split the line into arguments. Shel may make many changes to the arguments you entered while scanning the line. This behaviour of shel is called shell expamsion.
Shell will execute command after it scans and modifies it.
Removing white space
in shell commands parts are separated by one or more consecutive white spaces are considered as separate arguments. Any white space is remove.The very first arguments is the command to be executed and reset arguments are given to the command. Linux shell will effectively cut your command into onr or more arguments
below example will explain help in understanding this concept. We are typing different commands but their output is same after shell expansion process
This explains why the following different command lines are the same after shell
[root@shahid ~]# echo Today is Sunday Today is Sunday [root@shahid ~]# echo Today is Sunday Today is Sunday [root@shahid ~]# echo Today is Sunday Today is Sunday [root@shahid ~]# echo Today is Sunday Today is Sunday [root@shahid ~]#
Each arguments is displayed by echo command as it receives and will also add new white space between each argument
single quotes
If you want to keep extra whitespaces between arguments. then you can put contents in single quotes and it wil be considered one argument
see below example
[root@shahid ~]# echo 'Today is Sunday' Today is Sunday [root@shahid ~]#
double quotes
You can also use double quotes to avoid removing extra spacing in your line
[root@shahid ~]# echo "Today is Sunday" Today is Sunday [root@shahid ~]#
we will discuss in our later tutorial important differences between
single and double quotes.
echo and quotes
You can use echo -e command to use special escaped characters recognised by echo command in your quoted line. For example you can you use \n for a new line
and \t for tab. Below example helps to understand this concept
[root@shahid ~]# echo -e " This is challenging game. \n I am enjoying it" This is challenging game. I am enjoying it [root@shahid ~]# [root@shahid ~]# echo -e " this line has \t a tab" this line has a tab [root@shahid ~]#
You can see man pages for more special characters you can use in echo command
commands
There are two types of commands external or builtin commands.
All commands are not external to the shell, some of them are builtin.
What are External commands?
these commands have their own and located in the file system most of these external
commands are located in /bin or /sbin.
What are builtin commands?
These commands are integral part of the shell program itself
type
if you want to get information about command whether its external command or builtin you can use command type
[root@shahid ~]# type cd cd is a shell builtin [root@shahid ~]# type exit exit is a shell builtin [root@shahid ~]# type cat cat is /usr/bin/cat
Above example shows that cd command, exit command are shell builtin command where as cat command is located on file system located at /usr/bin/cat hence its external command. Type command is also useful in finding whete the command has alias or not
[root@shahid ~]# type ls ls is aliased to `ls --color=auto' [root@shahid ~]# alias ls alias ls='ls --color=auto'
running external commands
You will find some commands which will have both types builtin and external. If you will execute command priority will be to run builtin version.
And for some reason if you want to run external version you must need to specify full path of external command
[root@shahid ~]# type -a echo echo is a shell builtin echo is /usr/bin/echo [root@shahid ~]# /usr/bin/echo this is external command this is external command [root@shahid ~]#
which
The which command will search for binaries in the $PATH environment variable . We will discuss later about variables.
In below example you can see that cd is builtin, and which,mkdir,pwd,cp,mv,rm and pwd are external commands.
[root@shahid ~]# which which mkdir pwd,cp mv rm pwd alias mv='mv -i' /usr/bin/mv alias rm='rm -i' /usr/bin/rm alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' /usr/bin/alias /usr/bin/which /usr/bin/mkdir /usr/bin/which: no pwd,cp in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin) /usr/bin/pwd [root@shahid ~]#
aliases
create an alias
One of cool feature of shell is that it allows you to create an aliase for any shell command. These aliases are useful to create an easier and simpler to remember the name of existing command . Aliases are also help to easily supply parameters
[root@shahid ~]# ls -lh total 12K -rw-------. 1 root root 1.6K Feb 3 17:42 anaconda-ks.cfg -rwxr-xr-x. 1 root root 61 Mar 2 11:19 epoch_converter.pl -rw-r--r--. 1 root root 1.7K Feb 3 17:44 initial-setup-ks.cfg -rw-r--r--. 1 root root 0 Feb 23 12:50 test [root@shahid ~]# alias shahid='ls -lh' [root@shahid ~]# shahid total 12K -rw-------. 1 root root 1.6K Feb 3 17:42 anaconda-ks.cfg -rwxr-xr-x. 1 root root 61 Mar 2 11:19 epoch_converter.pl -rw-r--r--. 1 root root 1.7K Feb 3 17:44 initial-setup-ks.cfg -rw-r--r--. 1 root root 0 Feb 23 12:50 test [root@shahid ~]#
Aliases are also helpful in abrevating long command with long list of parameters
[root@shahid ~]# alias diskspace='du -h /home/shahid --max-depth=1' [root@shahid ~]# diskspace 0 /home/shahid/.mozilla 88K /home/shahid/.config 3.1M /home/shahid/.cache 0 /home/shahid/Desktop 0 /home/shahid/Downloads 0 /home/shahid/Templates 0 /home/shahid/Public 0 /home/shahid/Documents 0 /home/shahid/Music 0 /home/shahid/Pictures 0 /home/shahid/Videos 248K /home/shahid/.local 4.0K /home/shahid/security 8.0K /home/shahid/Stickyfolder 0 /home/shahid/groupid 3.5M /home/shahid [root@shahid ~]#
Another example can be that you supply commands with default options to alias. For example to to set the -i option default when typing rm.
[root@shahid ~]# echo this is testing > alias.txt [root@shahid ~]# rm -i alias.txt rm: remove regular file âalias.txtâ? no [root@shahid ~]# rm alias.txt rm: remove regular file âalias.txtâ? no [root@shahid ~]# type rm rm is aliased to `rm -i' [root@shahid ~]#
Did you notice when I typed without -i option it still prompts message whether you want to delete file or not.
Some distributions enable default aliases to protect users from deleting files.
we can specifically sue rm command without -i option by finding full path to original command by using type command
and will then delete file
[root@shahid ~]# which rm alias rm='rm -i' /usr/bin/rm [root@shahid ~]# /usr/bin/rm alias.txt [root@shahid ~]# ls alias.txt ls: cannot access alias.txt: No such file or directory [root@shahid ~]#
if there was no default you would create alias as shown in below example
[root@shahid ~]# alias rm='rm -i' [root@shahid ~]# touch alias.txt [root@shahid ~]# rm alias.txt rm: remove regular empty file âalias.txtâ?
viewing aliases
To view aliases you can use command alias (aliases name or list of alias names). Please see below examples. first example is showing definition of two aliases as we provide two arguments( actual aliases names) to alias command). Second one shows one definition as we have provided one alias name as argument to alias command
[root@shahid ~]# alias rm ll alias rm='rm -i' alias ll='ls -l --color=auto' [root@shahid ~]# alias ls alias ls='ls --color=auto' [root@shahid ~]#
unalias command
To remove alias you can use command unalias
[root@shahid ~]# alias rm ll alias rm='rm -i' alias ll='ls -l --color=auto' [root@shahid ~]# alias ls alias ls='ls --color=auto' [root@shahid ~]# unalias rm [root@shahid ~]# unalias ll [root@shahid ~]# alias rm ll -bash: alias: rm: not found -bash: alias: ll: not found [root@shahid ~]#
You can see in above example when you do alias rm ll it shows no aliases found for rm and ll
I am going to create alias again with alias command and check output
[root@shahid ~]# alias rm='rm -i' [root@shahid ~]# alias ll='ls -l --color=auto' [root@shahid ~]# alias rm ll alias rm='rm -i' alias ll='ls -l --color=auto' [root@shahid ~]#
To display shell expansion
YOu can use command set -x to display shell expansion and to stop display use set +x
You can display shell expansion with set -x, and stop displaying it with set +x. This helps when you want to know exactly what the shell is doing with your command.
[root@shahid ~]# set -x ++ printf '\033]0;%s@%s:%s\007' root shahid '~' [root@shahid ~]# echo * + echo alias.txt anaconda-ks.cfg epoch_converter.pl initial-setup-ks.cfg test alias.txt anaconda-ks.cfg epoch_converter.pl initial-setup-ks.cfg test ++ printf '\033]0;%s@%s:%s\007' root shahid '~' [root@shahid ~]# set +x + set +x [root@shahid ~]# echo * alias.txt anaconda-ks.cfg epoch_converter.pl initial-setup-ks.cfg test [root@shahid ~]#
When we enable shell expanion by set -x and then used command echo * it should how shell changed echo it to alias.txt anaconda-ks.cfg epoch_converter.pl initial-setup-ks.cfg test( * is wildcard means everything)
when we remove shell expansion by using set +x then issued command echo * it just printed output without displaying translation.
This is end of our tutorial I hope you have enjoyed. We have learnt Linux commands and arguments,external, builtin command, aliases, creating alias and removing alias and shell expansion.We demonstrated examples using echo command Thanks for visiting our blog and feel free to subscribe to our newsletter