Oscar Amezcua
Oscar Amezcua

Reputation: 21

Bash redirection operator sed command

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.

Upvotes: 2

Views: 68

Answers (2)

John Kugelman
John Kugelman

Reputation: 362037

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"

Upvotes: 3

Diego Torres Milano
Diego Torres Milano

Reputation: 69388

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

Upvotes: 0

Related Questions