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?

simpleCoder
  • 152
  • 2
  • 13
  • 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. – Charles Duffy Jun 02 '23 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). – Charles Duffy Jun 02 '23 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? – simpleCoder Jun 03 '23 at 00: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. – Charles Duffy Jun 03 '23 at 17:23
  • I'm considering a different tactic where I set a global alias with the text in the echo. – simpleCoder Jun 05 '23 at 15:42
  • bash doesn't have any such thing as a "global alias". Are you actually thinking about zsh instead? – Charles Duffy Jun 05 '23 at 16:00
  • Okay, this is now a duplicate of a _different_ question as it's been edited; you need to use single quotes if you don't want things expanded before they get to `echo`. – Charles Duffy Jun 05 '23 at 17:20

0 Answers0