1

I am trying to write a bash alias to execute a command, and echo it as well. Tried various methods mentioned in StackOverflow and other places, but nothing seems to work for this scenario.

My current alias (a much shortened example) is:

alias abc='__abc() { xyz -n $1 -m $2;}; __abc'

But, this doesnt echo the whole command making it difficult to understand what exact command was run.

Few ways I want to run the command:

abc 100 200

Add optional params:

abc 100 200 -o 300 -pp

And I expect the whole command including the optional params to be printed. So, it should print something like:

Executing: xyz -n 100 -m 200 -o 300 -pp

Please let me know if there is a way to do it. Thanks!

singleX
  • 375
  • 4
  • 13
  • I don't see how that alias echoes anything. Did you forget to put that part into the snippet? – Barmar Apr 14 '23 at 19:30
  • `echo abc "$@"` will echo all the arguments to the function. – Barmar Apr 14 '23 at 19:33
  • 2
    Why are you using an alias at all? You are redefining the function every time you use it. You can just define `abc () { xyz -n "$1" -m "$2"; }` *once*, and call it as many times as you like. – chepner Apr 14 '23 at 19:37
  • @Barmar I tried many examples from web. But none worked. So, I dont have an alias which echoes as well. – singleX Apr 14 '23 at 19:47
  • @chepner I am not a bash expert. I am following the alias file I got from my colleagues and trying to add to it :) – singleX Apr 14 '23 at 19:48
  • 2
    Ask your colleagues why they are using aliases instead of just using functions directly :) – chepner Apr 14 '23 at 20:05
  • 2
    For the bash newbies reading this; RTFM!! "For almost every purpose, aliases are superseded by shell functions." That line appeared in the documentation sometime before 1996!! Stop looking up how to use aliases. Stop emulating people using aliases. Right now, go edit `/etc/bash.bashrc` and just remove all the lines that define aliases. aliases have no place in the modern world. burninate them all – William Pursell Apr 14 '23 at 20:30
  • @WilliamPursell that might be true but I think I'll still use my `alias ll='ls -l'` and few other ones – Fravadona Apr 14 '23 at 21:05
  • @Fravadona You can get the functionality of that alias with `ll() { ls -l "$@"; }` Aliases cause far more confusion than they are worth. – William Pursell Apr 15 '23 at 12:29

1 Answers1

4

Use a function instead of alias. Then you can assign temporary variables from the required arguments, and use shift to remove them when echoing the command.

abc() { 
    n=$1
    m=$2
    shift 2
    echo "xyz -n '$n' -m '$m' $@"
    xyz -n "$n" -m "$m" "$@"
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • This doesnt seem to work the way I want. First of all, what I want to echo is the exact command which gets run, which is xyz, and not abc in this example. So, it shoud look like: xyz -n 100 -m 200. Sorry, I didnt make it clear earlier. Second of all, the parameter substitution is not happening correctly. – singleX Apr 17 '23 at 15:55
  • It wasn't clear from the question, since it never echoes anything. Why don't you just use `set -x` to see the command lines as they're being executed? – Barmar Apr 17 '23 at 16:00
  • Thanks. This is better, but still wont print the optional parameters (-o 300 -pp) I mentioned in my example. I tried set -x, but it dumps a whole lot of information as I am calling a python script (which I represented by xyz) which runs for minutes to hours. All I want to echo is the actual syntax of the python script which ran. – singleX Apr 17 '23 at 16:15
  • How is it supposed to know where the optional parameters belong? It sounds like you need to duplicate all the argument parsing of `xyz` in your `abc` function. – Barmar Apr 17 '23 at 16:16
  • Basically, the optional params will be called along with it's identifier. For example: abc 100 200 -o 300 -pp. So, anything after the first two params will be used as it is in the command substitution. – singleX Apr 17 '23 at 16:35
  • 1
    Now I get it. I've updated the answer. – Barmar Apr 17 '23 at 16:41