1

From Bash manual

3.7.1 Simple Command Expansion

When a simple command is executed, the shell performs the following expansions, assignments, and redirections, from left to right.

  1. The words that the parser has marked as variable assignments (those preceding the command name) and redirections are saved for later processing.

  2. The words that are not variable assignments or redirections are expanded (see Section 3.5 [Shell Expansions], page 21). If any words remain after expansion, the first word is taken to be the name of the command and the remaining words are the arguments.

  3. Redirections are performed as described above (see Section 3.6 [Redirections], page 31).

  4. The text after the ‘=’ in each variable assignment undergoes tilde expansion, parameter expansion, command substitution, arithmetic expansion, and quote removal before being assigned to the variable.

It mentions expansions happen on the following parts of a simple command: command name, command arguments, and assignments.

I was wondering if expansion also happens on redirection part? If yes, can you give some examples? Thanks.

  • I guess you are mislead by (2) where it says The words that are not variable assignments or redirections are expanded. Note that contrary to how we commonly talk about this, it is not the >c in the command a b >c which is the redirection in the sense of the bash man page. We are talking here about tokenizing the line into words. The words here are a, b, > and c, and it is just the word > which is the redirection word. – user1934428 Apr 9 '18 at 9:36
  • 1
    I wonder why this post got downvotes. IMO, the question makes sense (I, personally, find this part of the man page a bit confusing too), and the OP showed his effort to research about the topic by quoting the relevant part of the documentation. – user1934428 Apr 9 '18 at 9:38
0

Yes, if you redirect to/from the value of a variable or result of a command substitution, then that would have been expanded in step 2.

Example (creates the file called file):

outfile='file'
date >"$outfile"

You can not, however, do

redir='>'
date $redir file

as that would invoke date with the operands > and file. This is because when the command line was parsed in step 1, there was no "word that the parser had marked as a redirection" on it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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