1

What command will return whatever value is given to it. For example, you would enter the command followed by the string Hello World and it will print Hello World to the console.

Does such a command exist, and what is it?

1
  • echo (string)??
    – Danatela
    Apr 21, 2014 at 9:20

3 Answers 3

4
echo "Hello World "

or

printf "Hello World "

or (not for newbies):

str="Hello World" && "$str" |& grep -o "$str"
7
  • +1 for the printf and echo of course, but your third command does nothing. It would kinda work if $str contained no spaces (though it would be printed twice in cases where you get the "Did you mean X from package Y?" message) but as it is, what the shell attempts to execute is Hello World, which runs Hello, giving it World as an argument. Therefore, $str will never be present in the output and the grep will fail.
    – terdon
    Apr 19, 2014 at 23:40
  • @terdon Thanks and... Oh,... I wanted to be something just for fun. And for me $str is present in the output and grep doesn't fail; see here. If it bothers you and if you want to keep an emphasis sober, then edit the answer as you wish, I do not mind. Apr 19, 2014 at 23:58
  • OK, that's weird. That shouldn't be happening. I am guessing you've changed the value of IFS to something that doesn't include spaces. And of course it doesn't bother me, it just fails unless you've changed IFS (can't think of any other situation where it would work). What's the output of echo "$IFS" | od -a in the same terminal? Does it contain sp?
    – terdon
    Apr 20, 2014 at 0:07
  • @terdon Yes, it does. See here. And hmmm... This really is not for newbies :D Apr 20, 2014 at 0:22
  • Apparently not :). OK, I can get this to work in zsh but not in bash. One of us must have some setting changed or simply different bash versions.
    – terdon
    Apr 20, 2014 at 0:37
2

Do you mean echoing?

echo GoodMorning 

prints GoodMorning

1

In addition to the echo and printf shell builtins, if for some obscure reason you cannot use them, you also have cat. If you combine it with Here Strings, you can have it print the input you give it:

cat <<<"Hello World"

In fact, there are many commands that can do this if you twist their arm a little:

$ tr '' '' <<<"Hello World"
Hello World
$ sort  <<<"Hello World"
Hello World
$ tee  <<<"Hello World"
Hello World
$ xargs  <<<"Hello World"
Hello World
$ more  <<<"Hello World"
Hello World
$ fold  <<<"Hello World"
Hello World

Most of the above (and the list if far from complete) are designed to do something with their input and here we are asking them to basically do nothing, so they print it unchanged.

This answer is mostly for illustrative purposes and is a bit tongue-in-cheek. In real life, just use printf or echo, that's their job.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .