Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape dollar sign in string by shell script

Tags:

bash

shell

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.

echo '$1' // shows $1  echo "$1" // shows "sample.mp4"; want "sample$name.mp4" 

Then how to process $1 that I can detect whether there is a dollar sign in parameter $1

I want to process the string to sample\$name.mp4 or just detect whether there is a dollar sign in parameter $filename

like image 227
biubiubiu Avatar asked Jun 17 '16 08:06

biubiubiu


Video Answer


1 Answers

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' 
like image 157
pacholik Avatar answered Sep 19 '22 03:09

pacholik