You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I learned something cool about shell aliases today. Sometimes they don't work the way you expect, especially when you're trying to use live command results inside them.
I had this Git alias:
alias gitpul="git pull origin $(git branch --show-current)"
I thought it would always pull my current branch. But it kept trying to pull the same old branch, no matter which branch I was on.
This happened because aliases in the shell evaluate any command substitutions ($()) when the shell starts, not when the alias is executed.
The fix: Use single quotes for the alias definition and escape the inner double quotes:
alias gitpul='git pull origin "$(git branch --show-current)"'
This delays the command substitution until the alias is actually executed, ensuring it always pulls the current branch.
Tip
Single quotes in shell scripting delay evaluation because of how the shell interprets them. Let's break this down:
Double quotes (" "):
Allow variable and command substitution.
The shell expands variables and executes commands inside double quotes immediately.
Single quotes (' '):
Treat everything inside as literal text.
No expansions or substitutions occur inside single quotes.
The text was updated successfully, but these errors were encountered:
I learned something cool about shell aliases today. Sometimes they don't work the way you expect, especially when you're trying to use live command results inside them.
I had this Git alias:
I thought it would always pull my current branch. But it kept trying to pull the same old branch, no matter which branch I was on.
This happened because aliases in the shell evaluate any command substitutions ($()) when the shell starts, not when the alias is executed.
The fix: Use single quotes for the alias definition and escape the inner double quotes:
This delays the command substitution until the alias is actually executed, ensuring it always pulls the current branch.
Tip
Single quotes in shell scripting delay evaluation because of how the shell interprets them. Let's break this down:
The text was updated successfully, but these errors were encountered: