3

I'm using a program designed for windows that I compiled myself for GNU/linux. The program works in the command line, and one argument looks like this:

prgm -arg1 abcd(10)(22) -i files.ext

Where (10) and (22) are flags like a, b, c and d. This command is failing because bash tries to interpret stuff inside() and has a syntax error. So, I have two questions:

  1. What bash is actually trying to do with the parenthesis in this case? Why is it failing?
  2. I can change the source code, such as when the program recognizes another symbol. For example, I could use a curly brace instead of a parenthesis. What symbol would be safe?

2 Answers 2

11

Bash interpretes those as a list:

(list)

list is executed in a subshell environment (see COMMAND EXECUTION ENVIRONMENT below). Variable assignments and built-in commands that affect the shell's environment do not remain in effect after the command completes. The return status is the exit status of the list.


What symbol would be safe?

Bash uses a lot of characters and expects you to escape symbols. This is a list of symbols that have special meaning:

!#$%&'()*+,-./:;<=>?@[\]^_`{|}~

You need to escape ( and ) if those are characters (add \ in front)

So, in your case, the following will be interpreted as plain text:

prgm -arg1 abcd\(10\)\(22\) -i files.ext

Another method is to use quotes or double-quotes like this:

prgm -arg1 "abcd(10)(22)" -i files.ext
7
  • Thank you for your reply. I know I can escape the parenthesis but don't want to do it . It would not be convenient. I prefer to change the source code to use a safe symbol. What symbol would be safe?
    – JRR
    Sep 21 at 9:27
  • 1
    @JRR there are no safe symbols. prgm -arg1 "abcd(10)(22)" -i files.ext is another option
    – Rinzwind
    Sep 21 at 9:31
  • 11
    @JRR Don't use curly braces as those are used in bash for brace expansion ... Compare for ecample the output of echo abcd{10}{22} and echo abcd{10,20}{22}
    – Raffa
    Sep 21 at 9:40
  • 6
    @JRR and if you consider using [] instead, note that those are used for filename/wildcard expansion aka globbing, and if shopt -s nullglob is set, bash will silently eat your arguments. (Or simply error out on zsh, where globs that don't match anything is an error by default)
    – muru
    Sep 21 at 9:51
  • 1
    @JRR This is a well known issue. This is why you generally only want to use a very basic character set to name files, because otherwise it can become hard or at least inconvenient to write commands that use them. If you have something that is not just letters, numbers /.-_ chances are you should quote that thing in a command.... and you're often better off always using quotes so you don't forget
    – Bakuriu
    Sep 23 at 6:47
0

A common way to avoid the quoting issues would be to simply space-seperate the options, like this:

prgm -arg1 abcd 10 22 -i files.ext

In your program, you'd parse arguments until you encounter a -.

Another common option is to use commas:

prgm -arg1 abcd,10,22 -i files.ext
1
  • Thank you for your insight but the program is not mine and it is how it is.
    – JRR
    Sep 22 at 12:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .