alias not working inside bash shell script

8,885

Solution 1

Some comments:

  1. When writing a bash shell script you are expected to start the script with:

    #!/bin/bash
    
  2. There is a known issue - Why doesn't my Bash script recognize aliases? - that bash script doesn't recognize aliases.

One option to solve the issue is:

At the beginning of your script (after the #!/bin/bash) add:

shopt -s expand_aliases

Following by source of the file with the aliases:

source /etc/bash.bashrc

Solution 2

Bash aliases are usually not expanded in non interactive shells (which a script is). And really, you shouldn't rely on aliases in your scripts!

In the future you'll need the script on a different machine, and you'll forget about the aliases, and it won't work, and you'll waste time debugging the issue!.

Create your scripts to directly use the commands needed. Or create functions for complex or multi-line commands. Or better yet, create a "library" script with common used functions you need and then include that on your scripts.

That said, a simple way to call your script with all the aliases exported is to call it through an interactive bash session, via the -i flag:

$> bash -i ./tf.sh

Edit as per comment

A simple bash function wrapper for your python script would be:

function urldecode {
    PYTHON_ARG="$1" python - <<END
        import sys, urllib as ul
        print ul.unquote_plus(os.environ['PYTHON_ARG'])
    END
}
Share:
8,885
Born vs. Me
Author by

Born vs. Me

I debug and maintain email systems CodyCross Giornali Oggi

Updated on September 18, 2022

Comments

  • Born vs. Me
    Born vs. Me almost 2 years

    When I search for .bashrc files in my system I get:

    /etc/bash.bashrc
    /etc/skel/.bashrc
    /root/.bashrc
    /usr/share/base-files/dot.bashrc
    

    I changed:

    /etc/bash.bashrc   
    /root/.bashrc
    

    I added the alias.

    alias urldecode='python -c "import sys, urllib as ul; \
        print ul.unquote_plus(sys.argv[1])"'
    
    alias urlencode='python -c "import sys, urllib as ul; \
        print ul.quote_plus(sys.argv[1])"'
    

    When I run the command:

    urlencode 'http://example.com space'
    

    it works OK from the command line, but when I create an .sh file and put the same command there I get:

    ./tf.sh: line 19: urlencode: command not found
    

    What is wrong?

    Contents of tf.sh file:

    IP=$(curl -d "tool=checurl"-X POST https://site.com/get.py)
    url='https://site.com.com/'
    path=$(grep -oP '(?<=get.py\?filetype=csv\&data=).*?(?=\")' <<< $IP)
    pathfull="get.py?filetype=csv&data=$path"
    
    full=$url$pathfull
    
    #echo $full
    
    urlencode '$full'
    
    • Yaron
      Yaron over 5 years
      please show us the first lines of tf.sh
    • Born vs. Me
      Born vs. Me over 5 years
      @Yaron oooo I did
    • pLumo
      pLumo over 5 years
    • pLumo
      pLumo over 5 years
      You need to source the .bashrc in your script and make it interactive with set -i.
    • Born vs. Me
      Born vs. Me over 5 years
      How rovovovo? Show the full command please
  • Born vs. Me
    Born vs. Me over 5 years
    Just did that, didnt work
  • Born vs. Me
    Born vs. Me over 5 years
    The alias is a python command How can I make it to run it directlu onj the scrtipt? Imn happy with that
  • Daniele Santi
    Daniele Santi over 5 years
    @Bornvs.Me updated my answer
  • Born vs. Me
    Born vs. Me over 5 years
    IS the syntax ok? ./te.sh: line 11: warning: here-document at line 4 delimited by end-of-file (wanted `END') ./te.sh: line 12: syntax error: unexpected end of file
  • Daniele Santi
    Daniele Santi over 5 years
    @Bornvs.Me sorry, there should be NO spaces before END.
  • Born vs. Me
    Born vs. Me over 5 years
    File "<stdin>", line 1 import sys, urllib as ul ^ IndentationError: unexpected indent Python problem now :)
  • Daniele Santi
    Daniele Santi over 5 years
    @Bornvs.Me Sorry, don't know any python :D The text between <<END and END is passed verbatim to the command. Probably you want to remove all preceding spaces.
  • Born vs. Me
    Born vs. Me over 5 years
    The code doesn't worko