I have a csh script, which is executed using "source", and passes all its arguments to a program:
% alias foo source foo.csh
% cat foo.csh
./bar $*
# Some uninteresting stuff
If I run source foo.csh a b c
, all is OK. But not always:
foo "a b" "c d"
:
I expect bar
to get two arguments - a b
and c d
. Instead, it gets 4.
foo a "*" b
:
The *
is expanded to a list of files. I just want the character *
.
Extra credit - foo a * b
should work the same way. I know it's more problematic and I'm willing to live without it.
One thing I tried is changing ./bar $*
to ./bar "$*"
. This helps with the asterisk, but now bar
always gets everything in a single parameter.
Notes:
Our company uses csh as the login shell, so I must use it when using source
. Knowing that csh programming is considered harmful, I implemented all logic in bar
and left the bare minimum in the script.
If you suggest redefining the alias, it's important to see that redirection still works (foo | grep hello
), and that there's proper cleanup if ctrl-C breaks the script.
Arguments can be passed to the script when it is executed, by writing them as a space-delimited list following the script file name. Inside the script, the $1 variable references the first argument in the command line, $2 the second argument and so forth. The variable $0 references to the current script.
Passing arguments before runningWe can pass parameters just after the name of the script while running the bash interpreter command. You can pass parameters or arguments to the file.
When you type in a command at the Unix prompt, you are interacting with the shell. E.g., #!/bin/csh refers to the C-shell, /bin/tcsh the t-shell, /bin/bash the bash shell, etc. You can change your command shell with the chsh command.
You can also pass output of one shell script as an argument to another shell script. Within shell script you can access arguments with numbers like $1 for first argument and $2 for second argument and so on so forth.
Meanwhile, I've found the answer myself:
./bar $argv:q
The :q
modifier takes care of things. It passes to bar
the exact same parameters foo
got.
Source: http://www.staff.tugraz.at/reinfried.o.peter/unix/cshell.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With