Your script may be placing a blank string into $5
because there aren't five variables in your working directory path (pwd
), but it's hard to say without seeing the input string. If you want more help with this issue you will need to update your question with more information and show us what the input getting sent to awk
looks like.
I don't know if there is a way to make an alias dynamic, but you could simply convert each alias into a bash function. Bash functions take the form of funct_name () {commands}
, and they even take arguments which get stored in the variables $1
, $2
, $3
, ... etc. So, you could create a bash function which reads in the dynamic data as an argument. For example:
test () {
echo $1
}
Will behave as follows:
$ test abc
abc
$ test foobar
foobar
With this method, you would take the alias command and place it where echo
is in my example, using $1
instead of $CLIENT
. However, you don't even have to recreate each alias as a bash function and use arguments in this way.
From the sample commands you gave me, it looks like you are taking a folder name from your working directory path and storing it into an environment variable to use it in various aliases. You could simply create a bash function to do this task for you. As an example here is a bash function which will take the path of your working directory, find the first folder name (you seem to want the 4th so change this), and save it to the $TEST
environment variable.
test () {
var=$(pwd | awk -F / '{ print $2; }')
export TEST=$var
}
If you want this function to be available to you always, add the above into your .bashrc
file. Anytime you start a new terminal window the test
command will be at your disposal. If you want to add it to your current working terminal (instead of having to open a new one), run source ~/.bashrc
. You can then run the function in the terminal like it is a command. You will just have run test
and the environment variable will update and your alias commands should work. Here is an example of it in action:
$ pwd
/home/user
$ echo $TEST
$ test
$ echo $TEST
home
Be aware: this command depends on which directory you are working in. If you change folders to root, my example will place an empty string into $TEST
because there is no string right after the first slash. This may be why awk
is printing an empty string in your case, but again, I can't say for sure without seeing the input data.