Skip to main content
The 2024 Developer Survey results are live! See the results
edited tags
Link
Kusalananda
  • 339.1k
  • 37
  • 684
  • 992
added 329 characters in body
Source Link
JS.
  • 111
  • 4

Is there a way to display aliases after bash has made the substitutions?

Background:
I have a long alias that performs actions on a directory.

alias do_it='cd my_working_dir; do_stuff'

This works fine, but the alias is quite long and my_working_dir changes often. Rather than do a in-depth search on my alias for all occurrences of my_working_dir, I changed to this:

MY_WORKING_DIR=some_dir
alias do_it='cd ${MY_WORKING_DIR}; do_stuff'

This works fine.

However, sometimes I want to make slight changes to the alias on the fly. With the previous alias construction, I was able to type alias, copy/paste the command I wanted, make mods and execute. Using this substitution, when I type alias I get:

do_it='cd ${MY_WORKING_DIR}; do_stuff'

Now I have to manually replace MY_WORKING_DIR. Is there a handy way to get a copy of the alias after bash has made the substitutions (in this case the substitution for ${MY_WORKING_DIR}.

Clarification: My goal is not change the inputs to the alias/function. I wish to display the resulting alias/function with substitutions already made so that I can copy/paste that output to make more complex commands. I have yet to find a way to display either aliases or functions with the substitutions already made.

Additional Example

TARGET_DIR='my_dir'    
alias do_it='cd ${TARGET_DIR}; run task; echo "Task done in ${TARGET_DIR}'
alias
do_it='cd ${TARGET_DIR}; run task; echo "Task done in ${TARGET_DIR}'

If I want to repeat the alias 50x, I could copy the alias output, then paste it into the following line:

for i in {1..50}; do <paste_here>; done

But that won't work because the alias output has left ${TARGET_DIR} unresolved.

Using a function instead of an alias does not work either because declare likewise does not replace ${TARGET_DIR}.

This is a simplified example. The real aliases I am using are long and tedious, with ${TARGET_DIR} occurring many times, so while the alias/function works great, composing the alias output into more complex on-the-fly commands requires me to manually replace ${TARGET_DIR} multiple times.

Is there a way to display aliases after bash has made the substitutions?

Background:
I have a long alias that performs actions on a directory.

alias do_it='cd my_working_dir; do_stuff'

This works fine, but the alias is quite long and my_working_dir changes often. Rather than do a in-depth search on my alias for all occurrences of my_working_dir, I changed to this:

MY_WORKING_DIR=some_dir
alias do_it='cd ${MY_WORKING_DIR}; do_stuff'

This works fine.

However, sometimes I want to make slight changes to the alias on the fly. With the previous alias construction, I was able to type alias, copy/paste the command I wanted, make mods and execute. Using this substitution, when I type alias I get:

do_it='cd ${MY_WORKING_DIR}; do_stuff'

Now I have to manually replace MY_WORKING_DIR. Is there a handy way to get a copy of the alias after bash has made the substitutions (in this case the substitution for ${MY_WORKING_DIR}.

Clarification: My goal is not change the inputs to the alias/function. I wish to display the resulting alias/function with substitutions already made so that I can copy/paste that output to make more complex commands. I have yet to find a way to display either aliases or functions with the substitutions already made.

Is there a way to display aliases after bash has made the substitutions?

Background:
I have a long alias that performs actions on a directory.

alias do_it='cd my_working_dir; do_stuff'

This works fine, but the alias is quite long and my_working_dir changes often. Rather than do a in-depth search on my alias for all occurrences of my_working_dir, I changed to this:

MY_WORKING_DIR=some_dir
alias do_it='cd ${MY_WORKING_DIR}; do_stuff'

This works fine.

However, sometimes I want to make slight changes to the alias on the fly. With the previous alias construction, I was able to type alias, copy/paste the command I wanted, make mods and execute. Using this substitution, when I type alias I get:

do_it='cd ${MY_WORKING_DIR}; do_stuff'

Now I have to manually replace MY_WORKING_DIR. Is there a handy way to get a copy of the alias after bash has made the substitutions (in this case the substitution for ${MY_WORKING_DIR}.

Clarification: My goal is not change the inputs to the alias/function. I wish to display the resulting alias/function with substitutions already made so that I can copy/paste that output to make more complex commands. I have yet to find a way to display either aliases or functions with the substitutions already made.

Additional Example

TARGET_DIR='my_dir'    
alias do_it='cd ${TARGET_DIR}; run task; echo "Task done in ${TARGET_DIR}'
alias
do_it='cd ${TARGET_DIR}; run task; echo "Task done in ${TARGET_DIR}'

If I want to repeat the alias 50x, I could copy the alias output, then paste it into the following line:

for i in {1..50}; do <paste_here>; done

But that won't work because the alias output has left ${TARGET_DIR} unresolved.

Using a function instead of an alias does not work either because declare likewise does not replace ${TARGET_DIR}.

This is a simplified example. The real aliases I am using are long and tedious, with ${TARGET_DIR} occurring many times, so while the alias/function works great, composing the alias output into more complex on-the-fly commands requires me to manually replace ${TARGET_DIR} multiple times.

added 329 characters in body
Source Link
JS.
  • 111
  • 4

Is there a way to display aliases after bash has made the substitutions?

Background:
I have a long alias that performs actions on a directory.

alias do_it='cd my_working_dir; do_stuff'

This works fine, but the alias is quite long and my_working_dir changes often. Rather than do a in-depth search on my alias for all occurrences of my_working_dir, I changed to this:

MY_WORKING_DIR=some_dir
alias do_it='cd ${MY_WORKING_DIR}; do_stuff'

This works fine.

However, sometimes I want to make slight changes to the alias on the fly. With the previous alias construction, I was able to type alias, copy/paste the command I wanted, make mods and execute. Using this substitution, when I type alias I get:

do_it='cd ${MY_WORKING_DIR}; do_stuff'

Now I have to manually replace MY_WORKING_DIR. Is there a handy way to get a copy of the alias after bash has made the substitutions (in this case the substitution for ${MY_WORKING_DIR}.

Clarification: My goal is not change the inputs to the alias/function. I wish to display the resulting alias/function with substitutions already made so that I can copy/paste that output to make more complex commands. I have yet to find a way to display either aliases or functions with the substitutions already made.

Is there a way to display aliases after bash has made the substitutions?

Background:
I have a long alias that performs actions on a directory.

alias do_it='cd my_working_dir; do_stuff'

This works fine, but the alias is quite long and my_working_dir changes often. Rather than do a in-depth search on my alias for all occurrences of my_working_dir, I changed to this:

MY_WORKING_DIR=some_dir
alias do_it='cd ${MY_WORKING_DIR}; do_stuff'

This works fine.

However, sometimes I want to make slight changes to the alias on the fly. With the previous alias construction, I was able to type alias, copy/paste the command I wanted, make mods and execute. Using this substitution, when I type alias I get:

do_it='cd ${MY_WORKING_DIR}; do_stuff'

Now I have to manually replace MY_WORKING_DIR. Is there a handy way to get a copy of the alias after bash has made the substitutions (in this case the substitution for ${MY_WORKING_DIR}.

Is there a way to display aliases after bash has made the substitutions?

Background:
I have a long alias that performs actions on a directory.

alias do_it='cd my_working_dir; do_stuff'

This works fine, but the alias is quite long and my_working_dir changes often. Rather than do a in-depth search on my alias for all occurrences of my_working_dir, I changed to this:

MY_WORKING_DIR=some_dir
alias do_it='cd ${MY_WORKING_DIR}; do_stuff'

This works fine.

However, sometimes I want to make slight changes to the alias on the fly. With the previous alias construction, I was able to type alias, copy/paste the command I wanted, make mods and execute. Using this substitution, when I type alias I get:

do_it='cd ${MY_WORKING_DIR}; do_stuff'

Now I have to manually replace MY_WORKING_DIR. Is there a handy way to get a copy of the alias after bash has made the substitutions (in this case the substitution for ${MY_WORKING_DIR}.

Clarification: My goal is not change the inputs to the alias/function. I wish to display the resulting alias/function with substitutions already made so that I can copy/paste that output to make more complex commands. I have yet to find a way to display either aliases or functions with the substitutions already made.

Source Link
JS.
  • 111
  • 4
Loading