Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to write shell script that takes an argument, and then applies it to files.

Specifically, I want to give a term, and then have it compile with mxmlc ("mxmlc"), then run term.swf with flashplayerdebugger ("flashplayerdebugger term.swf"). I'm fairly new to shell scripting - any thoughts?

share|improve this question
$2 is the first argument, $3 the second, and so on. $1 is reserved for the name of the file. As for the other stuff, I don't quite understand what you mean. Could you please try to explain it a little better or write some psuedocode? – InkBlend Aug 15 '12 at 23:36
@InkBlend you're off by one. "$1" is the first argument. – geirha Aug 17 '12 at 13:20
@geirha: Ouch. That hurts. You're right, of course. Silly me, I must have been thinking of Python. – InkBlend Aug 17 '12 at 17:16
up vote 12 down vote accepted

You could use something like this:

# $0 is the script name, $1 id the first ARG, $2 is second...
mxmlc $
flashplayerdebugger $NAME.swf
share|improve this answer
Or skip the NAME variable altogether and just use mxmlc "$1".as and flashplayerdebugger "$1".swf. (Quotes can be inside expressions.) – Eliah Kagan Aug 15 '12 at 23:49
but what if the param contains spaces ? – Dawid Aug 15 '12 at 23:50
You're quite right. I've edited my comment to fix this error. – Eliah Kagan Aug 15 '12 at 23:53
Thank you very much! – southpointingchariot Aug 16 '12 at 0:34
@shinnra If the parameter contains spaces, then nxmlc and flashplayerdebugger will get multiple arguments instead of the intended one argument. The expansion of NAME needs to be double-quoted. On a side note, don't use all uppercase variable names; you risk overwriting special shell variables or environment variables. – geirha Aug 16 '12 at 19:05

I also recommend you use the variable name delimiter. So the code would look like:

# $0 is the script name, $1 id the first ARG, $2 is second...
mxmlc ${NAME}.as
flashplayerdebugger ${NAME}.sw

This allows the use of the variable in any context, even inside other text. For example:

NewName="myFileIs${NAME}and that is all"

This would expand the variable NAME which would be flanked in front by "myFileIs" and at the back with "and that is all" The variable would expand, spaces included, inside the string. if NAME was "inside here" the NewName would be "myFileIsinside hereand that is all".

The command line can take up to 9 variables. They can be quoted strings which contain blanks, each quoted string counts as a variable. Such as:

./myProg var1 var 2 var3

So ${1} is "var1", ${2} is "var", ${3} is "2", ${4} is "var3"

BUT: ./myProg var1 "var 2" var3

has ${1} is "var1", ${2} is "var 2", ${3} is "var3"

Have fun!

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.