Bash command as variable

前端 未结 5 1051
一个人的身影
一个人的身影 2021-01-24 02:43

I am trying to store the start of a sed command inside a variable like this:

sedcmd=\"sed -i \'\' \"

Later I then execute a command like so:

5条回答
  •  故里飘歌
    2021-01-24 03:32

    It works when the command is only one word long:

    $ LS=ls
    $ $LS
    

    But in your case, the shell is trying the execute the program sed -i '', which does not exist.

    The workaround is to use $SHELL -c:

    $ $SHELL -c "$LS"
    total 0
    

    (Instead of $SHELL, you could also say bash, but that's not entirely reliable when there are multiple Bash installations, the shell isn't actually Bash, etc.)

    However, in most cases, I'd actually use a shell function:

    sedcmd () {
        sed -i '' "$@"
    }
    

提交回复
热议问题