Bash redirection operator sed command

Viewed 39

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.

2 Answers

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"
Related