How to use brace expansion ({}) to spread shell command arguments (#tilPost)

Today I was reading a discussion about mv. The linked gist includes a shell custom function that wraps the mv command and adds functionality if the user only provides one argument.
# wrapped mv command which accepts edits to
# the …


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Today I was reading a discussion about mv. The linked gist includes a shell custom function that wraps the mv command and adds functionality if the user only provides one argument.

# wrapped mv command which accepts edits to 
# the provided file path if only one argument was provided
mv planet.png
planet2.png
planet.png -> planet2.png

It allows for editing the file path of provided first argument interactively. That is very neat by itself.

I went on a read the discussion about this command and learned that the mv command, or to make it clearer, bash/zsh make allow to rename a file with something that looks like a single command.

mv a/very/long/path/file.txt a/very/long/path/renamed.txt

You can shorten the above command to the following:

mv a/very/long/path/{file,renamed}.txt

Common shells will extract the {} pattern into its parts and create separate arguments.

mv a/very/long/path/{file,renamed}.txt
# ? becomes ?
mv a/very/long/path/file.txt a/very/long/path/renamed.txt

Another example is the creation of files that include indices.

touch {1..3}.txt
# ? becomes ?
touch 1.txt 2.txt 3.txt

Shell scripting. ? There is always something new to learn.

Edited: Dominik pointed out that this shell feature is called brace expansion.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2020-04-18T22:00:00+00:00) How to use brace expansion ({}) to spread shell command arguments (#tilPost). Retrieved from https://www.scien.cx/2020/04/18/how-to-use-brace-expansion-to-spread-shell-command-arguments-tilpost/

MLA
" » How to use brace expansion ({}) to spread shell command arguments (#tilPost)." Stefan Judis | Sciencx - Saturday April 18, 2020, https://www.scien.cx/2020/04/18/how-to-use-brace-expansion-to-spread-shell-command-arguments-tilpost/
HARVARD
Stefan Judis | Sciencx Saturday April 18, 2020 » How to use brace expansion ({}) to spread shell command arguments (#tilPost)., viewed ,<https://www.scien.cx/2020/04/18/how-to-use-brace-expansion-to-spread-shell-command-arguments-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » How to use brace expansion ({}) to spread shell command arguments (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2020/04/18/how-to-use-brace-expansion-to-spread-shell-command-arguments-tilpost/
CHICAGO
" » How to use brace expansion ({}) to spread shell command arguments (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2020/04/18/how-to-use-brace-expansion-to-spread-shell-command-arguments-tilpost/
IEEE
" » How to use brace expansion ({}) to spread shell command arguments (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2020/04/18/how-to-use-brace-expansion-to-spread-shell-command-arguments-tilpost/. [Accessed: ]
rf:citation
» How to use brace expansion ({}) to spread shell command arguments (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2020/04/18/how-to-use-brace-expansion-to-spread-shell-command-arguments-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.