2

I am defining an alias in a departmental .bashrc file, which is sourced before the individual user .bashrc file. In the individual user .bashrc file I would like to be able to add options to the existing alias. So for example in the first (departmental) .bashrc file I have the following:

alias my_command = '/usr/local/bin/my_command -option1 -option2' 

In the user .bashrc file I'd like to build on the existing alias, e.g.

alias my_command = 'my_command -option3' 

As written, the alias instruction in the user .bashrc file would simply replace the alias of the same name defined in the departmental .bashrc file. I tried to get around this with the following approach:

existing_alias_components=($(type my_command)) 

which returns the words my_command, is, aliased, and to in the first four elements of the array. I want to capture the remaining array elements, join them back into a string -- say $string -- and then issue the command

alias my_command="$string -option 3" 

I'm running into challenges with the fact that the quote characters that surround the first definition of the alias are getting into the first and last array values after the first four. I can substring them out, but it's getting to be a lot of work for what would be a one-liner in perl. Is there a more expedient way to accomplish this?

Thanks!

2
  • 3
    Are you sure you don't want to use a function for this? Commented May 21, 2016 at 0:22
  • 1
    Why would you want to pre-resolve the path? The shell caches PATH lookups, so there's not any performance enhancement from doing so. Commented May 21, 2016 at 0:26

2 Answers 2

2

Consider using a function instead. For instance:

my_command_args=( -option1 -option2 ) my_command() { command my_command "${my_command_args[@]}" "$@" } 

...after which someone can run:

# add a 3rd option on the end: my_command_args+=( -option-3 ) 

...or, to prepend a new argument:

# add a 0th option on the beginning my_command_args=( -option0 "${my_command_args[@]}" ) 

The big advantage here is that you can append data to your alias without needing to safely quote it. For instance, the following would be very bad news with an alias:

# This is evil bad_string='$(rm -rf /tmp)' alias my_command="$oldalias --fail-if-command-contains=$bad_string" 

...needing instead to be run through something like printf '%q':

# ...can make it safer this way, if using an alias bad_string='$(rm -rf /tmp)' printf -v bad_string_safe %q "$bad_string" alias my_command="$oldalias --fail-if-command-contains=$bad_string_safe" 

...but it would be perfectly safe without that step if using the function formulation:

# This is safe bad_string='$(rm -rf /tmp)' my_command_args+=( --fail-if-command-contains="$bad_string" ) 
Sign up to request clarification or add additional context in comments.

Comments

1

I'd simply extract the alias value from between the single quotes:

 oldalias=$(alias my_command|sed "s/^[^']*'//; s/'$//"); 

and then do

 alias my_command="$oldalias -option 3" 

1 Comment

Excellent, thanks to everyone who replied, all provided very insightful feedback with great explanations. Greatly appreciated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.