Question

I was looking over some ksh code and came across a line I didn't think would work. Upon testing the code, it did work, but I am not sure why.

If it matters, this is running on an AIX 5.3 system.

The line in the code was:

scriptname=$(\basename $0)

I didn't think it would work because of the backslash in front of the basename command, but when I tested the scriptname variable, it had the correct value.

I did some further testing on the command line:

$ echo $(\echo test)
test
$ echo $(\echo \test)
test
$ echo $(\e\c\h\o \test)
test
$ echo `\e\c\h\o \test`
test
$ echo $(\echo "\test")
est

I tried it outside of the command subtitution, and it still didn't work like I thought it would:

$ \echo \testi\ng
testing
$ \echo "\testi\ng"
        esti
g

The last one is the only one that makes some sense to me.

Does Kornshell just ignore the backslashes, or maybe just convert them to the character in front of the backslash? I assumed that putting a \n anywhere on the command line would be a newline, but apparently only if it is in quotes, or \\n.

Can someone explain what ksh does with backslashes on the command line?

Either way, I assume the better way to write the command I originally questioned would be:

scriptname=$(basename $0)

instead of:

scriptname=$(\basename $0)

Does it matter?

Thanks in advance!

Was it helpful?

Solution

If any part of a command name is quoted then ksh will ignore any aliases when selecting what to execute.

I presume that is why the original script was using \basename.

OTHER TIPS

I ran into a related but slightly different situation. I am editing a shell script with several rm commands written as:

\rm ...

I was wondering why the '\' was before the rm and similarly tried it only to find that it does seem to work. Per this site, this is done to prevent the script from accidentally using an alias (e.g. if rm is aliased to 'rm -i'). Since I found the discussion on Stack Overflow first, I thought I would add this bit in case it helps the next person out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top