Reuse Commands with Shell Arguments

My arguments for arguments, functions and alias


We often want to test the output with different values for a parameter. Here’s an example: we have a parameter that pandoc icon pandoc uses to / compile source code files to PDF / M$ Word / etc.

rm output.html; param=0; pandoc input$param.txt -o output.html; \
echo param = $param

In practice, a parameter can be a font, the font size, etc, and there can be multiple parameters.


To change the value in the parameter (e.g. 0 in param=0) in the above command, one has to move the cursor to the middle—that is a tedious thing to do. It would be much more convenient to put the value for the parameter at the end of the command, like the following line.

rm output.html; pandoc input$param.txt -o output.html; param=value

However, the $param isn’t taking the value at the end of the above command. Is there a way to use param with a value to be specified at the end of the line?


My first idea was to invert the shell’s character parsing order. However, as an end user of any shell (e.g. Bash, Zsh, Sh), I couldn’t extract anything useful.

Then I thought about the idea of mathematical functions, which quickly translated to shell scripts. Unluckily, the idea of creating another file to be made executable (by chmod +x) might not satisfy users seeking one-liners. Besides, that might not be a good idea for users accessing remote shells because a remote user might lack the permissions to run the command chmod +x.


Shells supporting function calls

I have tested function calls with Git Bash. The following works for many well-known shells like Bash, Zsh, Sh, etc.

my_func () { rm temp.txt; touch temp.txt && echo input$1.txt; }
my_func 1
# output: input1.txt
my_func 2
# output: input2.txt

Here I declared a shell function my_func, and $1 refers to the first argument passed to my_func.

Shells supporting function calls

Some shells like (t)csh don’t support function calls. I tried rewriting the above shell function with an alias.

alias helloWorld 'echo input$1.txt; echo do other stuff'

However, on tcsh, that’s not working.

hellowWorld first_argument
# outputs:
# input.txt
# do other stuff first_argument

Thanks to this Stack Overflow answer about tcsh alias, I know that $1 is passed to the script containing the alias rather than the alias itself. I should use \!:1 instead of $1.

alias helloWorld 'echo input\!:1.txt; echo do other stuff'
hellowWorld first_arg
# outputs:
# inputfirst_arg.txt
# do other stuff
linux  tcsh 

No comment

Your email address will not be published. Required fields are marked *.