Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash: create nested alias

Tags:

alias

bash

I am trying to make an alias as follows, where app_folder is the top level folder and appX is the executable:

alias app_folder='/tmp/app'
alias app1='app_folder/app1'
alias app2='app_folder/app2'
alias app_output='cat app_folder/log.txt'

when i call this in the terminal i get something like this:

bash: app_folder/app1: No such file or directory
cat: app_folder/log.txt: No such file or directory

I've tried all different methods but none of them work:

app1="$(app_folder)"'/app1'
app1='`app_folder`/app1'

so is this possible using aliases, if not, is there any other way?

like image 593
7331skillz Avatar asked Aug 09 '26 12:08

7331skillz


1 Answers

I think you want something like this:

APP_FOLDER="/tmp/app"
alias app1="$APP_FOLDER/app1"
alias app2="$APP_FOLDER/app2"
alias app_output="cat $APP_FOLDER/log.txt"

If this changes regularly, you can use this in a file, e.g., to change which folder your aliases are pointing:

APP_FOLDER="$1"
alias app1="$APP_FOLDER/app1"
alias app2="$APP_FOLDER/app2"
alias app_output="cat $APP_FOLDER/log.txt"

So if this was in an "aliastest" file, you could do:

. aliastest /tmp/app

And you would get, e.g.:

$ alias | grep app
alias app1='/tmp/app/app1'
alias app2='/tmp/app/app2'
alias app_folder='/tmp/app'
alias app_output='cat /tmp/app/log.txt'

Is this what you're looking for? Alternately, you can include variables in your aliases using single quotes, e.g.:

alias app1='$APP_FOLDER/app1'
alias app2='$APP_FOLDER/app2'
alias app_output='cat $APP_FOLDER/log.txt'

Then, whenever you set the APP_FOLDER variable, your aliases will use it.

like image 187
zerodiff Avatar answered Aug 11 '26 02:08

zerodiff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!