How can I split a shell command over multiple lines when using an IF statement?

后端 未结 2 1748
再見小時候
再見小時候 2020-11-28 00:51

How can I split a command over multiple lines in the shell, when the command is part of an if statement?

This works:

if ! fab --fabfile         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 01:48

    The line-continuation will fail if you have whitespace (spaces or tab characters[1]) after the backslash and before the newline. With no such whitespace, your example works fine for me:

    $ cat test.sh
    if ! fab --fabfile=.deploy/fabfile.py \
       --forward-agent \
       --disable-known-hosts deploy:$target; then
         echo failed
    else
         echo succeeded
    fi
    
    $ alias fab=true; . ./test.sh
    succeeded
    $ alias fab=false; . ./test.sh
    failed
    

    Some detail promoted from the comments: the line-continuation backslash in the shell is not really a special case; it is simply an instance of the general rule that a backslash "quotes" the immediately-following character, preventing any special treatment it would normally be subject to. In this case, the next character is a newline, and the special treatment being prevented is terminating the command. Normally, a quoted character winds up included literally in the command; a backslashed newline is instead deleted entirely. But otherwise, the mechanism is the same. Most importantly, the backslash only quotes the immediately-following character; if that character is a space or tab, you just get a literal space or tab, and any subsequent newline remains unquoted.

    [1] or carriage returns, for that matter, as Czechnology points out. Bash does not get along with Windows-formatted text files, not even in WSL. Or Cygwin, but at least their Bash port has added a set -o igncr option that you can set to make it carriage-return-tolerant.

提交回复
热议问题