I'm trying to write a function that creates an alias and writes it to a file. I wrote it so that the user enters the name for the alias as $1 and the path to travel to with the alias as $2.
I have 2 variables $alias_name
& $path_to_add
that store $1 $2 respectively. Then I have a third variable $jp_string
that looks like this:
`jp_string="alias $alias_name='cd $path_to_add'"
The function checks if both arguments are present and if so uses them to fill in the spaces of $jp_string then writes $jp_string to a file. Everything works fine except when I run the script the word 'alias' in my string gets run as a command instead of being treated as just a string. How do I write the $jp_string variable so that it will be parsed as just a string in double quotes and not commands? Any help is appreciated I looked elsewhere for answers and kept getting close but not quite. I learned that you need to escape special symbols like '$' as $ in order to use it in a string but how do you "escape" commands like 'alias' or 'cd' in a string?
So in this function if I pass in for example "myalias" and "my/new/path" as args $1 and $2. This is the output error I get:
addPathToJumpPoints:11: alias myalias='cd ' not found
function addPathToJumpPoints() {
alias_name=$1
path_to_add=$2
xjp_file=$XFILES/.xtest
jp_string=""
if [[ ! -z $alias_name ]]; then
if [[ ! -z $path_to_add ]]; then
$jp_string="alias $alias_name='cd $jp_path'"
if [[ -d $path_to_add ]]; then
echo $jp_string >> $xjp_file
echo "Jump point to: [$path_to_add] was created!"
else
echo "[$path_to_add] does not exist, would you like to create it? (y/n)"
read make_path
if [[ $make_path == "y" ]]; then
mkdir $path_to_add
echo $jp_string >> $xjp_file
echo "$path_to_add and a jump point to it were created!"
elif [[ $make_path == "n" ]]; then
echo "Should a jump point be made regardless? (y/n)"
read make_jp
if [[ $make_jp == "y" ]]; then
echo $jp_string >> $xjp_file
echo "Jump point to [$path_to_add] was created!"
elif [[ $make_jp == "n" ]]; then
echo "No jump point was created."
else
echo "Invalid answer!. Enter only 'y' for yes and 'n' for no."
fi
else
echo "Invalid answer. Enter only 'y' for yes and 'n' for no."
fi
fi
else
echo "Please provide a path to create a jump point"
fi
else
echo "Please provide an alias and a path to create a jump point!"
fi
}