simple (and stupid) shell scripting question

Nerius Landys nlandys at gmail.com
Mon Feb 15 20:16:02 UTC 2010


>> #!/bin/sh
>>
>> DIRNAME="`dirname \"$0\"`"
>> cd "$DIRNAME"
>> SCRIPTDIR="`pwd`"
>>
>> What if I got rid of extra double quotes?  Like this:
>>
>> DIRNAME=`dirname \"$0\"`
>> cd "$DIRNAME"
>> SCRIPTDIR=`pwd`
>
> That is perfectly fine.  Word-splitting and filename expansion are
> not performed for variable assignments.  Also immune is the expression
> after "case", so this is always fine:
>
>  case $FOO in ...

Since you guys have been so helpful I thought I'd ask one more question.

Let's say I have a script named "te st" (yes, with a space in the filename).
The contents of this script are:

#!/bin/sh
if [ $# -ne 2 ]; then
    echo 1>&2 "One argument expected.  Example usage:"
    echo 1>&2 "  `basename \"${0}\"` 64.156.192.169 cheaters"
    exit 1
fi

When executed with no arguments, this script would print something like so:

nlandys at daffy<zsh># ./te\ st
One argument expected.  Example usage:
  te st 64.156.192.169 cheaters
nlandys at daffy<zsh>#

Is there a function, or command line utility, to "escape" a string,
making it suitable to be input on the command line?  For example, this
"escape" utility would take a input of "te st" and create an output of
"te\ st".  Other things such as quotes and single quotes would be
handled as well.


More information about the freebsd-questions mailing list