Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent expressions enclosed in backticks from being evaluated in heredocs [duplicate]

Tags:

I have a text like this:

foo bar `which which` 

If I do this using heredoc, I get a blank file:

➜  ~  echo <<EOT > out heredoc> foo bar heredoc> `which which` heredoc> EOT ➜  ~  cat out  ➜  ~   

How can I do this?

Edit

Oh sorry, I meant to do cat. Problem is that it writes this to the file: which: shell built-in command, ie, evaluations backticks. Any way to do this without evaluating?

With cat, I get

➜  ~  cat <<EOT > out heredoc> foo bar heredoc> `which which` heredoc> EOT ➜  ~  cat out foo bar which: shell built-in command ➜  ~   

I don't want which which to be evaluated.

like image 287
user1527166 Avatar asked Oct 29 '12 12:10

user1527166


People also ask

What do Backticks mean in bash?

The basic functionality of backticks The Open Group has a definition for the backtick operator, officially referred to as command substitution. This operator is not the single quote character, but another character known as the grave accent ( ` ).

What does back quote do in bash?

The back quote is the one to use when you want to assign output from system commands to variables. It tells the shell to take whatever is between the back quotes as a system command and execute its output. Using these methods you can then substitute the output into a variable.

How does HereDoc work in bash?

A HereDoc is a multiline string or a file literal for sending input streams to other commands and programs. HereDocs are especially useful when redirecting multiple commands at once, which helps make Bash scripts neater and easier to understand.


1 Answers

Quote the label to prevent the backticks from being evaluated.

$ cat << "EOT" > out foo bar `which which` EOT  $ cat out foo bar `which which` 
like image 149
dogbane Avatar answered Sep 20 '22 06:09

dogbane