0

I have a bash script that is writing out a file to /usr/local/bin that needs to take in additional parameters.

I'm trying something like this:

  echo "PYTHONPATH=/opt/script_folder/$TIMESTAMP /opt/script_folder/$TIMESTAMP/script -param <param_value> $@" | sudo tee /usr/local/bin/script_alias

I want the user to be able to use this by running script_alias -second_param <second_value> ... and the script picking up the second parameter.

However, the $@ is pulling in every param I pass to the shell script writing out the /usr/local/bin file. I tried escaping the $ to \$@ but the $@ is not being written out. Any thoughts on why?

7
  • 1
    To quote the bash IRC factoid database: "If you have to ask, use a function instead". Aliases are prefix substitution -- just simple string replacement; that's all they can do. Commented Jun 2, 2023 at 23:08
  • (That means that when you use "$@" or "$1" or any other reference to command-line arguments in an alias, you're not referring to the arguments that the alias was passed, but the arguments that were already set before you called the alias; the alias doesn't have its own stack frame, so it doesn't have its own argument list). Commented Jun 2, 2023 at 23:10
  • My challenge is that there are values I want to pass into the bash script to hardcode into the alias/function but then I want it to also take additional command line arguments. Do you know where I can learn to do that? Commented Jun 3, 2023 at 0:13
  • args=( "$@" ) will store your current argument list into an array, so you can have a function have its own argument list but be able to refer back to that array. BTW, aliases don't work in scripts at all. Commented Jun 3, 2023 at 17:23
  • I'm considering a different tactic where I set a global alias with the text in the echo. Commented Jun 5, 2023 at 15:42

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.