开发者

Backslashes in Kornshell

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!


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜