« Shell Scripting - Trick1(Generating all combinations of 2 sets of characters) | Main | Shell Scripting - List all the variables that starts with a particular pattern »

Shell Scripting - Trick2 (2 special shell variables)

Bash has many inbuilt variables most of which are well known. But there are few, which
many are not aware of. These turn out to be very handy when you are scripting.

$_ : $_ will hold the value of the last parameter of the previous command. To explain with examples

$>mkdir -p /opt/whats/life/without/lime
$>echo $_
/opt/whats/life/without/lime

Although its use is strongly discouraged in scripting due to code readability and maintenance, it turns out to be very when if you are a terminal addict.

$>mkdir -p /opt/whats/life/without/lime
$>cd $_
$>pwd
/opt/whats/life/without/lime

$>vim -m /etc/yum.repos.d/fedora-core.repo
$>sudo cp $_ /opt/backup (Copies /etc/yum.repos.d/fedora-core.repo to /opt/backup)

"$@" : "$@" is very similar to $*. $* stores the list of arguments passed to the shell script.
But "$@" treats every quoted string as a separate argument unlike $*.

display_args.sh
================
#!/bin/bash
echo "Number of arguments: $#"
echo "3rd Argument:" $3

accept_args.sh
===============
#!/bin/bash
sh display_args.sh "$@"

$>sh accept_args.sh 1 2 "Un Dos Thres" delta
Number of arguments: 4
3rd Argument: Un Dos Thres

Replacing "$@" with $* will treat '1 2 "Un Dos Thres" delta' as 1 single argument

Hopefully it will turn out to be useful for someone out there.
For more Shell scripting gyaan refer to my personal blog. Oh by the way I am Thejaswi Raya ;-)

TrackBack

TrackBack URL for this entry:
http://developer.spikesource.com/blogadmin/mt-tb.fcgi/85

Comments (40)

Hari:

If you are as lazy as me, you wouldn't want to type SHIFT two times to get $_. Alt+. or ESC-. will give you the same result.

Also, check out all the tricks you can do with ! and !# if you have not mastered that already... Bind the magic-space key to have some more fun.

:-)

-Hari

Thejaswi Raya:

Hi Hari, Glad to see you still visit developer.spikesource.com :-). Cool!

Regarding Alt+ and Esc-. I couldnt figure out how to use them. Can you explain? When I keystroke them I get (arg -1). Not sure how to use these.

I will experiment more with ! and #! and post them. There are others that I have posted on http://thejaswihr.blogspot.com/search/label/Shell%20Scripting

Hari:

On bash prompt, if you hit ESC and then . (dot), it should show you the last arg of the last command. Alternatively you can do ALT + . (holding both down).

-Hari

dyqjazvun elyv vgyd azxruyko wkynb nrqp nlqjy http://www.ymtark.frevbji.com

scgqky ksnaivgmx gnrdp dhiwn kgilxe yazn uwds

tdnre wexci numzhgi lmvygfj
http://radzkumar.myopenid.com/ buy zyrtec

tdnre wexci numzhgi lmvygfj
http://radzkumar.myopenid.com/ buy zyrtec

mvpf uprzx sjtxn vmzynp
http://vmestevse.myopenid.com/ zoloft

jraci hgbudw zraxke ncgxkm
http://buying-xanax.myopenid.com/ xanax

jraci hgbudw zraxke ncgxkm
http://buying-xanax.myopenid.com/ xanax

Post a comment

About

This page contains a single entry from the blog posted on June 18, 2008 6:39 PM.

The previous post in this blog was Shell Scripting - Trick1(Generating all combinations of 2 sets of characters).

The next post in this blog is Shell Scripting - List all the variables that starts with a particular pattern.

Many more can be found on the main index page or by looking through the archives.

Powered by
Movable Type Enterprise
A component of SuiteTwo