0

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
}


  • 1
    To get some useful hints add a suitable shebang (`#!/bin/bash`) to your script and then paste it at http://www.shellcheck.net/. – Cyrus Apr 24 '23 at 03:34
  • Hey that is an extremely helpful tool thanks so much for that link. I'll debug the script and run it again if anything I'll check back. Appreciate it Cyrus. – Paul McCarthy Apr 24 '23 at 03:42

2 Answers2

0

This line

$jp_string="alias $alias_name='cd $jp_path'"
  1. To declare a String, do like STRING_NAME="some string"
  2. I don't see jp_path defined anywhere. Shouldn't it be the input $2, i.e. path_to_add?

Try updating the line to the following:

jp_string="alias $alias_name='cd $path_to_add'"
Taylor G.
  • 661
  • 3
  • 10
  • S@!* lol You are right jp_path isn't defined its supposed to be $path_to_add. let me see if fixing that helps. Thanks for pointing that out. I've been staring at this function too long. I looked it over and over and didn't catch that. I need to take a break. – Paul McCarthy Apr 24 '23 at 03:58
  • I fixed and when I pass in "myalias" and "my/new/path" i'm still getting this: addPathToJumpPoints:11: alias myalias='cd my/new/path' not found – Paul McCarthy Apr 24 '23 at 04:04
0

Okay so I figured out the issue. I really appreciate the help from everyone though. I was using '$' during assignment

$jp_string="alias $alias_name='cd $path_to_add'"

I should have done this:

jp_string="alias $alias_name='cd $path_to_add'"

  • 3
    Please don't post a duplicate answer which repeats the information in the answer you received. Instead, accept the answer (and probably delete this redundant one, and upvote the answer which helped you). – tripleee Apr 24 '23 at 05:00