0

Can somebody tell me how to print variable and not executing it in bash? I mean:

bash@bash $ cat script.sh 
#!/bin/bash
echo $1

bash@bash $ ./script.sh "`date`"
Sat Sep 20 18:42:19 CEST 2014

I don't want to get:

Sat Sep 20 18:35:37 CEST 2014

I want to get output:

date

I'm interested in how to prevent executing sent commands to script.

5
  • 1
    Replace your back ticks with single quotes. Commented Sep 20, 2014 at 12:43
  • wrap with single quotes Commented Sep 20, 2014 at 12:43
  • Thank you for answer, but i made a mistake. I want to get "echo date" Commented Sep 20, 2014 at 12:46
  • 1
    Then use single quotes on the first and double quotes on the second line. Commented Sep 20, 2014 at 12:46
  • Don't do that. Commented Sep 20, 2014 at 13:53

2 Answers 2

3

It looks like you are trying to prevent code from being injected into your script. The problem with echo $1 is that the contents of $1 are being evaluated by the shell. In order to avoid that, you need to wrap $1 in double quotes in your script:

#!/bin/bash
echo "$1"

Testing it out:

$ ./script.sh '`date`'
`date`

The problem in your question is that you are using double quotes around "date", so the expansion has occurred before your script is run. You can use set -x to see the difference:

$ set -x
$ ./script '`date`'
+ ./script '`date`' 
`date`
$ ./script "`date`"
++ date                                   # date is being run
+ ./script 'Sat Sep 20 18:01:32 BST 2014' # result is passed to your script
Sat Sep 20 18:01:32 BST 2014

There is nothing you can do about this.

I think the following section of my original answer is still relevant, so I'll leave it in.

Different types of quotes in bash

Backticks (which you have used in your question) are an old-fashioned way of capturing the output of executing a command. The more modern syntax is $( ), so if you wanted to store the current date in a variable you could do d=$(date). Single quotes are used for literal strings, so echo '$d' would output $d, not the value of the variable. Variables inside double quotes are expanded, so echo "$d" would output the value of the variable $d. It is always a good idea to wrap your variables in double quotes to prevent word splitting and glob expansion.

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Tom, It could be unclear.Sorry, All I want to get is not executing commands but print it. You wrote "variable1=date". But what about when somebody pass to variable1 string `date` ?
Sorry, last comment was not properly showed. string `date`
I hope that now it will be clear. I'm interested in how to prevent executing sent commands to script.
"The problem in your question is that you are using double quotes around "date", so the expansion has occurred before your script is run. You can use set -x to see the difference:" Thank you very much, now I see a difference! :)
0

Replace the backticks from var1 with single quotes:

var1='date'
var2="echo $var1"
echo $var2

1 Comment

Your second line is bogus.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.