%%bash echo "Current directory is $(pwd)"
audrey.feldroy.com
The experimental notebooks of Audrey M. Roy Greenfeld. This website and all its notebooks are open-source at github.com/audreyfeldroy/audrey.feldroy.com
# Command Substitution in Bash
by Audrey M. Roy Greenfeld | Wed, Jan 1, 2025
## Simple Command Substitution
Use $()
not backticks
It also works with backticks, but it's not the best way:
%%bash echo "Current directory is `pwd`"
(Because when you nest these, you have to escape the backticks)
Example: Moving Untracked Files to _drafts
Folder
%%bash git ls-files --others --exclude-standard
%%bash mv $(git ls-files --others --exclude-standard) ./_drafts
It works for me and I'd be happy stopping here, but in the real world where files have spaces, it'll break.
Fancy Command Substitution With xargs
xargs
lets you map a list to any command. Here I would use it like:
%%bash git ls-files --others --exclude-standard | xargs -I {} mv {} ./_drafts/
The untracked files list is piped to xargs
, which lets us run a command for each line in the list. Then mv
is run for each file in the list.
-I {}
is needed to put the filename somewhere other than the end of the mv
command. Here, -I
enables string replacement, and {}
is the placeholder string that gets replaced.
© 2024-2025 Audrey M. Roy Greenfeld