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?
echo "Hello World "
or
printf "Hello World "
or (not for newbies):
str="Hello World" && "$str" |& grep -o "$str"
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.
$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
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
?
zsh
but not in bash. One of us must have some setting changed or simply different bash versions.
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.
echo (string)
??