What is the different between echo $(pwd) and echo "$(pwd)"?
What is the different between echo $(pwd) and echo "$(pwd)"?
I remember that they are the same thing, but the two commands give me different output.
Here is my output:
/usr/share/locale ⌚ 21:19:24
$ echo $(pwd) usr shar l cal
/usr/share/locale ⌚ 21:19:32
$ echo "$(pwd)"
/usr/share/localeIt seems like many characters are dropped by echo $(pwd), so many scripts can not be run correctly. For example, I must use eval "$(something --alias)" instead of eval $(something --alias).
I tried echo $(pwd) and echo "$(pwd)" in a docker container, the outputs are same.
Is there something wrong in my system?
1 Answer
It's all about word splitting, there is a variable named IFS: internal field separator, after an expansion or substitution bash will splits the results of that process into different words based on IFS's value.
The default value of this variable is: "<space><tab><newline>", means words separated by space or tab or a newline will be considered and passed as separate words to your command.
So a variable with the value of "hello name is foo", after expansion will be considered as 4 different word:
$ printf "%s\n" hello name is foo
hello
name
is
fooIf I want to consider it as a single word I should quote it:
$ printf "%s\n" "hello name is foo"
hello name is fooIf I set the IFS to / it will use / as field separator, so:
$ export IFS='/'
$ var=/some/thing/here
$ echo $var some thing hereThat might be your case (having a / in IFS), however if you quote the subsituation like: "$( ... )" then bash will skip the word splitting.
then try running your commands again, now it shouldn't use / to separate words including in the path (pwd).