Wednesday, 11 November 2009

echo command

This is referenced from the link


prints (to stdout) an expression or variable (see Example 4-1).

echo Hello
echo $a

An echo requires the -e option to print escaped characters. See Example 5-2.

Normally, each echo command prints a terminal newline, but the -n option suppresses this.


An echo can be used to feed a sequence of commands down a pipe.

if echo "$VAR" | grep -q txt # if [[ $VAR = *txt* ]]
echo "$VAR contains the substring sequence \"txt\""


An echo, in combination with command substitution can set a variable.

a=`echo "HELLO" | tr A-Z a-z`

See also Example 12-19, Example 12-3, Example 12-42, and Example 12-43.

Be aware that echo `command` deletes any linefeeds that the output of command generates.

The $IFS (internal field separator) variable normally contains \n (linefeed) as one of its set of whitespace characters. Bash therefore splits the output of command at linefeeds into arguments to echo. Then echo outputs these arguments, separated by spaces.

bash$ ls -l /usr/share/apps/kjezz/sounds
-rw-r--r-- 1 root root 1407 Nov 7 2000
-rw-r--r-- 1 root root 362 Nov 7 2000

bash$ echo `ls -l /usr/share/apps/kjezz/sounds`
total 40 -rw-r--r-- 1 root root 716 Nov 7 2000 -rw-r--r-- 1 root root 362 Nov 7 2000

No comments:

Post a Comment

Tweets by @sriramperumalla