0

This following command prints nothing on my machine

echo `python3.8 -c 'print ("*"*10)'`

whereas

python3.8 -c 'print ("*"*10)' 

does. Why?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
zell
  • 9,830
  • 10
  • 62
  • 115
  • 1
    What OS/shell? I get `**********` in zsh (macOS default) and a list of the things in my current directory in bash, for example. – jonrsharpe Oct 25 '21 at 13:21
  • Unless you are trying to embed the output of Python in a larger string, capturing the output only to write it to standard output again with `echo` isn't necessary. – chepner Oct 25 '21 at 13:32
  • Similar: [When should I wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable); relevant: [Security implications of forgetting to quote a variable in bash/POSIX shells](https://unix.stackexchange.com/questions/171346/security-implications-of-forgetting-to-quote-a-variable-in-bash-posix-shells) – glenn jackman Oct 25 '21 at 14:17

2 Answers2

3

The first example does command substitution for the argument to echo and is equivalent to the command echo **********. This happens to output a list of directory contents, since ********** seems to be equivalent to *1 and is expanded by the shell.

If you want to prevent the shell to expand **********, you need to quote it:

echo "`python3.8 -c 'print ("*"*10)'`"

which is equivalent to echo "**********".


1don't quote me on that (pun intended)

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
1

`expr` means to evaluate the contents between ' as a command and replace it with the result.

`print(*)` is equal to ` * ` which is evaluated as all the files in the current directory

The output really depends on the machine used

Mar
  • 167
  • 1
  • 8