moyoka
moyoka

Reputation: 27

Alias command incorrectly executing

I am attempting to load my git aliases from a gist on github. For some reason, the command executes find, but when I attempt to execute any of the aliases, they are either incorrectly mapped — e.g., gsts -> git stash instead of gsts -> git status — or they are not mapped at all.

 #!/bin/bash  

update_git_aliases(){
        GIST_URL='https://gist.githubusercontent.com/Moyoka22/ec605b0b52fee6d6d30d5f72822938f4/raw/git-aliases'

        RESPONSE="$(wget --no-cache -qO- ${GIST_URL})"

        if [ ${?} -ne 0 ]
        then
                echo 'Download failed. Exiting.'
                return 1
        fi

        echo ${RESPONSE} > ${1} 
        chmod +x ${1}
}

DOWNLOAD_FAILED=0
ALIAS_FILE="${HOME}/.config/git-aliases"


if [ ! -f ${ALIAS_FILE} ]
then
        echo "Git aliases not found! Downloading..."
        update_git_aliases ${ALIAS_FILE}
        DOWNLOAD_FAILED=${?}
fi

if [ ${DOWNLOAD_FAILED} -ne 0 ]
then 
        echo "Downloading aliases failed."
        exit 1
fi

cat ${ALIAS_FILE} | bash 

Upvotes: 0

Views: 194

Answers (2)

Schwern
Schwern

Reputation: 164629

The alias commands must be run in your current shell process to have an effect.

cat ${ALIAS_FILE} | bash executes the alias commands in ALIAS_FILE in a new child process, not your shell, and not the program's shell.

source runs the commands in the current shell. You need to source the file from your current shell, not from the program. You can do this after the file is updated. In order to make this permanent, you will need to add source "${HOME}/.config/git-aliases" to your shell config.

What many programs like this do is print out the necessary commands at the end.

echo "$ALIAS_FILE updated"
echo "Make sure `source $ALIAS_FILE` is in your $HOME/.bash_profile"
echo "Run `source $ALIAS_FILE` to use it in your current shell"

Upvotes: 1

Diego Torres Milano
Diego Torres Milano

Reputation: 69189

I assume you want the aliases on your interactive shell, so remove the last cat as it's useless and once ${HOME}/.config/git-aliases is created

$ source "${HOME}/.config/git-aliases"

Upvotes: 2

Related Questions