Bash-operators

From LQWiki
Jump to: navigation, search

Substitution Operators

Substitution Operators are substituted by what they stand for.

$()

Imagine you got that little command line from bash_tips that outputs your IP-address:

duffman:~ # ifconfig eth1 | sed -rn 's/^.*inet addr:([^ ]+).*$/\1/p'
192.168.0.6

Now, you want your IP address stored into a variable, say, you want to substitute the commandline by its output and store it to a variable. Use the operator $() to do this:

duffman:~ # ip=$(ifconfig eth1 | sed -rn 's/^.*inet addr:([^ ]+).*$/\1/p' )
duffman:~ # echo $ip
192.168.0.6
duffman:~ #         

The operator $() executes the statement that is within its paranthesis and substitutes itself with the output of that statement. Backticks (``) do the same, but they are not cascadable.

$(())

$((content)) is substituted by the arithmetic evaluation of content.

*

The operator * is substituted by all files in your current folder, e.g.

ls *

behaves the same as

ls file1 file2 file3...

Evaluating Operators

Evaluating Operators deliver a return code in dependance from their content. Example:

[[ $a = $b ]]

delivers 0 if $a is $b, else, it delivers a non-null return code.

Redirecting Operators

A redirect or redirection is the mechanism with that output is sent to a different destination than its default. You can redirect the output of commands or your own input.

>

For instance, echo's standard output is directed to the console whereas the shell, with a redirection operator can send that to e.g. a file.

$ echo foo > bar

sends foo to bar rather than the screen. bar is an ordinary file, but can also be a device.

>>

$ echo foo >> bar

will append the string "foo" to the ordinary file "bar".

$ echo foo > bar

is short for

$ echo foo 1> bar

which means that the stream 1, stdout will be redirected to bar.

1>

You can redirect output from any command:

$ ls 1> myfiles

will create a file "myfiles" and store the output of ls there. Please note that not everything that you see is in the stdout stream. Take for example:

duffman:~ # ls a
/bin/ls: a: No such file or directory
duffman:~ # ls a 1>/dev/null
/bin/ls: a: No such file or directory
duffman:~ #                

The redirection does not work here - obviously, the error message ("No such file or directory") has been sent to another stream than stdout. It has been sent to stderr (Standard Error Stream). The stream's numbers are as follows:

2>

So, the following redirects the error message to /dev/null:

duffman:~ # ls a 2>/dev/null
duffman:~ #

In the following, there is an example of a command (ls text a) that outputs one line to stderr and one to stdout. You can find there how to unite both streams (stdin and stdout).

duffman:~ # ls text a
/bin/ls: a: No such file or directory
text
duffman:~ # ls text a 2>/dev/null
text
duffman:~ # ls text a 1>/dev/null
/bin/ls: a: No such file or directory
duffman:~ # ls text a 2>&1 | wc -l
2
duffman:~ #                   

2>&1

The redirection "2>&1" makes that the error messages are sent to stdout as well. You can count the number of lines produces with a pipe and wc. Now the following example sends all output from stdout and stderr to a file:

duffman:~ # ls text a >text 2>&1
duffman:~ # cat text
/bin/ls: a: No such file or directory
text
duffman:~ #

>file<<

The command cat > file << EOF will redirect the stdin-stream to the ordinary file "file". It will not stop sending the input there till the input is "EOF". So, the following example will create a file "text" with 3 lines:

duffman:~ # cat >text<<END
> my first line
> my second line
> my last line before END
> END
duffman:~ #  

|

See piping

$ variables

$!

$! evaluates to the PID of the last process that was sent to the background:

tweedleburg:~ # sleep 60 &
[1] 14655
tweedleburg:~ # jobs
[1]+  Running                 sleep 60 &
tweedleburg:~ # echo $!
14655
tweedleburg:~ # kill $!
tweedleburg:~ # jobs
[1]+  Terminated              sleep 60

See also