Escape dollar sign in string by shell script

前端 未结 5 683
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 11:42

Suppose I have a script named dd.sh, and I run it like this

./dd.sh sample$name.mp4

So $1 is the string sample$name.mp4

5条回答
  •  青春惊慌失措
    2020-12-01 12:26

    As you know, a dollar sign marks a variable. You have to take it into account when you are typing it.

    You can escape the dollar

    ./dd.sh "sample\$name.mp4"
    

    or just type it with single quotes

    ./dd.sh 'sample$name.mp4'
    

    To check if there is a dollar sign in a variable, do

    [[ $variable == *\$* ]] && echo 'I HAZ A DOLAR!!!' || echo 'MEH'
    

提交回复
热议问题