Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using parentheses in bash commands, git mv

Tags:

bash

git-bash

Trying to rename a file using parentheses but I keep hitting this error and can't find any docs around it:

-bash: syntax error near unexpected token `('

I'm doing something like:

git mv title_1.1 title_(1.1)
like image 892
jpn Avatar asked Sep 21 '26 01:09

jpn


2 Answers

Parentheses are special to shells -- they're used when defining function, declaring arrays, using command substitution and for subshells.

To use them literally, as in filenames, just add appropriate quoting:

git mv title_1.1 "title_(1.1)"

Note Mat's warning though. This is a medium risk filename that may cause certain poorly written tools and scripts to choke. It's better than having spaces, but worse than being alphanumeric.

like image 149
that other guy Avatar answered Sep 23 '26 18:09

that other guy


Parentheses are used to denote a subshell in under posix, so they are a reserved word under sh, bash, ksh etc. To tell the shell that you want parentheses not to act as part of the shell syntax, you can either quote them:

git mv title_1.1 "title_(1.1)"

git mv title_1.1 'title_(1.1)'

or you can escape them:

git mv title_1.1 title_\(1.1\)

However, this is often not taken into account by shell scripts, meaning that lots of them will break when trying to handle file names which use parentheses.

It is considered good practice to avoid file names which do not match the regex [-.a-zA-Z0-9_], i.e. Alphanumeric characters, dashes, dots and underscores.

like image 42
Barton Chittenden Avatar answered Sep 23 '26 18:09

Barton Chittenden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!