Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!apple!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!hp4nl!star.cs.vu.nl!maart From: maart@cs.vu.nl (Maarten Litmaath) Newsgroups: comp.unix.questions Subject: Re: Variable substitition Message-ID: <8393@star.cs.vu.nl> Date: 30 Nov 90 20:09:31 GMT References: <1990Nov30.092424@cs.utwente.nl> Sender: news@cs.vu.nl Reply-To: maart@cs.vu.nl (Maarten Litmaath) Organization: VU Dept. of Computer Science, Amsterdam, The Netherlands Lines: 44 In article <1990Nov30.092424@cs.utwente.nl>, stadt@cs.utwente.nl (Richard van de Stadt) writes: )Is there some sort of variable substitution possible in a shell script )to get the value of the last argument supplied to the script? I don't )mean shifting the arguments until one is left. I'd like to know if something )like awk's $NF, in which NF means the number of fields, and $NF means the )value of the last field, is possible. ${$#} results in an error message. # How to get the last argument of a shell script reliably. # If you are sure there are fewer than 10 arguments, you can use this: # # eval echo \"\$$#\" # # Instead of ``echo "$foo"'' you could use the portable echo hack: # # expr "$foo" : '\(.*\)' # # Alternatively: # # cat << EOF # $foo # EOF set a b c d e f g h i j k l m -n # for example case $# in 0) last= ;; *) last=` n=$# set 0 ${1+"$@"} shift $n echo -n "$1" ` esac echo "last=|$last|" -- "Please DON'T BREAK THE CHAIN! Terry Wood broke the chain and ended up writing COBOL PROGRAMS. Three days later, he found his Blue Star Tatoo Letter, made 20 copies and mailed them out. He found a good job writing compilers." -- tjw@unix.cis.pitt.edu (Terry J. Wood)