Home > Net >  Bash redirection operator sed command
Bash redirection operator sed command

Time:06-07

I am going through a Google Cloud training which is using a bash script to perform sed string replacement and then deploying to endpoint services. There is this bash line that I just can't wrap my head around...

mv "$temp_file" "$TEMP_FILE"
# Because the included API is a template, we have to do some string
# substitution before we can deploy it. Sed does this nicely.
< "$API_FILE" sed -E "s/YOUR-PROJECT-ID/${project_id}/g" > "$TEMP_FILE"

Can someone explain what the redirection operator is doing before "$API_FILE" expansion? Is this another way of writing:

sed -E "s/YOUR-PROJECT-ID/${project_id}/g" "$API_FILE" > "$TEMP_FILE"

I am very confused with the position of the redirection operator "<" at the beginning of the line. The deployment is working so this isn't a syntax issue.

CodePudding user response:

Most people put redirections at the ends of commands but they can actually appear anywhere. They can be at the beginning, or even in the middle of other arguments (not recommended, but legal!).

These are all equivalent:

< "$API_FILE" sed -E "s/YOUR-PROJECT-ID/${project_id}/g" > "$TEMP_FILE"
sed < "$API_FILE" -E "s/YOUR-PROJECT-ID/${project_id}/g" > "$TEMP_FILE"
sed -E < "$API_FILE" "s/YOUR-PROJECT-ID/${project_id}/g" > "$TEMP_FILE"
sed -E "s/YOUR-PROJECT-ID/${project_id}/g" < "$API_FILE" > "$TEMP_FILE"

CodePudding user response:

Redirection operators may precede or appear anywhere within a simple command or may follow a command. Redirections are processed in the order they appear, from left to right.

See the manual: https://www.gnu.org/software/bash/manual/html_node/Redirections.html

  • Related