Pro Backup
Pro Backup

Reputation: 761

Bash one liner: How to nest curly braces for list generation inside a curly brace command group?

The goal is to create a one line (copy and paste) bash command. The bash one line command should partition and format a drive, and when that results in a mountable volume, some initial maintenance commands.

The goal is to execute a list of commands when cd /Volumes/VolumeName succeeds in the current shell context. This grouping of commands can be done in bash by using curly braces. When cd /Volumes/VolumeName fails (echo $? != 0) further command execution is not necessary and command execution can stop.

The result of command cd /Volumes/$VOL && {sudo rm -fr .{,_.}{fseventsd,Spotlight-V100,Trashes}; mkdir .fseventsd;} is:

-bash: syntax error near unexpected token `}'

The bottleneck might be that one of the commands in the curly braced command list is using curly braces for list generation: sudo rm -fr .{,_.}{fseventsd,Spotlight-V100,Trashes}.

How to nest curly braces for list generation inside a curly brace command group?

Upvotes: 1

Views: 1637

Answers (3)

kojiro
kojiro

Reputation: 77147

You're just missing a necessary leading space. Consider:

mini:~ michael$ { echo sudo rm -fr .{,_.}{fseventsd,Spotlight-V100,Trashes}; echo mkdir .fseventsd;}
sudo rm -fr .fseventsd .Spotlight-V100 .Trashes ._.fseventsd ._.Spotlight-V100 ._.Trashes
mkdir .fseventsd
mini:~ michael$ {echo sudo rm -fr .{,_.}{fseventsd,Spotlight-V100,Trashes}; echo mkdir .fseventsd;}
-bash: syntax error near unexpected token `}'

If you look in man 1 bash under the Compound Commands section you'll see this (emphasis mine):

{ list; }

list is simply executed in the current shell environment. list must be terminated with a newline or semicolon. This is known as a group command. The return status is the exit status of list. Note that unlike the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to be recognized. Since they do not cause a word break, they must be separated from list by whitespace.

Upvotes: 4

matteomattei
matteomattei

Reputation: 660

Maybe the problem is with the nested braces. Try with this:

cd /Volumes/$VOL && (sudo rm -fr .{,_.}{fseventsd,Spotlight-V100,Trashes}; mkdir .fseventsd;)

If you want to use a subshell, the rigth way is to use "()" instead of "{}". Using "()" you don't need to add an extra space at the beginning of the command.

Upvotes: 0

nshy
nshy

Reputation: 1074

Add spaces around braces that are used to command grouping. { sudo ... insead of {sudo ...

Upvotes: 0

Related Questions