Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In zsh, ${$(pwd)} output the current directory. However, if I run echo ${/path/to/somewhere}, it just shows an empty line. Why does this happen? Thanks!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I assume, you speak about print ${$(pwd)} or something like that, right?!

The expansion of ${$(pwd)} to the current working directory is the expected behavior, although it might look strange at the first glance. Don't let you fool by thinking that the command substitution $(pwd) gets evaluated first an the result is used as a variable name... no, it is used as the variable value as man zshexpn explains:

${name} The value, if any, of the parameter name is substituted.

(...) If a ${...} type parameter expression or a $(...) type command substitution is used in place of name above, it is expanded first and the result is used as if it were the value of name. (...) The form with $(...) is often useful in combination with the flags described next; see the examples below.

(...) The flag f is useful to split a double-quoted substitution line by line. For example, ${(f)"$(<file)"} substitutes the contents of file divided so that each line is an element of the resulting array.

To your second example. I think this is the pattern replace syntax:

${name/pattern/repl} Replace the longest possible match of pattern in the expansion of parameter name by string repl. (...)

See this example first:

$ foo=hello
$ print ${foo/hello/good/bye}
good/bye

If you omit foo you tell zsh to do a search and replace in the variable ${}, which is empty:

$ print ${}

$

-- hence ${/something/foo/bar} returns an empty string, too.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.