Bash Shell Learnings

The Linux shell performs several kinds of Expansions

[0]  Word-splitting:

$ echo this is a     test
this is a test

  • word-splitting by the shell removed extra whitespace from the echo command’s list of arguments

[1]  Pathname Expansion  :  *

Note: Word-splitting and Pathname expansion can sometimes be the cause of frustration during command execution. See Quoting below, to understand how use of single / double quotes help.

[2] Tilde Expansion :  ~

[3] Arithmetic Expansion : $

  • Arithmetic expansion uses the form:  $((expression))

[4] Brace Expansion : {}

[5] Parameter Expansion : $

[6] Command Substitition : $,  “`”  (backquote)

  • Command substitution allows us to use the output of a command as an expansion
  • ls -l $(which cp)
  • ls -l `which cp`

Quoting:

Quoting is the way we can get control over the shell.

[1]  Double Quotes :

If we place text inside double quotes, all the special characters used by the shell lose their special meaning and are treated as ordinary characters.

The exceptions are “$”, “\” (backslash), and “`” (back- quote).

  • Because $ is an exception, it means that parameter expansion, arithmetic expansion, and command substitution are still carried out
  • However,  things like word-splitting, pathname expansion, tilde expansion, and brace expansion are suppressed.
  • This makes it favourable for using in commands like find. 

[2]  Single Quotes :

Suppress all expansions.

$ echo text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER
text /home/me/ls-output.txt a b foo 4 me

$ echo "text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER"
text ~/*.txt {a,b} foo 4 me

$ echo 'text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER'
text ~/*.txt {a,b} $(echo foo) $((2+2)) $USER

[3]  Escaping Characters : 

  • Sometimes you only want to quote a single character. To do this, you can precede a character with a backslash, which in this context is called the escape character. Often this is done inside double quotes to selectively prevent an expansion:

$ echo "The balance for user $USER is: \$5.00"
The balance for user me is: $5.00

  • Backslash allows us to embed newlines in our command. Note that for this trick to work, the newline must be typed immediately after the backslash. If you put a space after the backslash, the space will be ignored, not the newline

$ ls -l \
> –reverse \
> –human-readable \
> –full-time
total 3.0K
-rwxrwx—+ 1 agoswami Domain Users 57 2015-12-17 17:21:02.810929400 -0800 file3.txt
-rwxrwx—+ 1 agoswami Domain Users 18 2015-12-17 17:13:41.974365200 -0800 file2.txt
-rwxrwx—+ 1 agoswami Domain Users 21 2015-12-17 18:07:29.022681300 -0800 file1.txt

  • Backslashes are also used to insert special characters into our text. These are called backslash escape characters. e.g.
    • \n : Adding blank lines to text
    • \t : Inserting horizontal tabs to text
    • \\ : Inserts a backslash

Stdout and Stderr:

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s