alias not working inside bash shell script












0















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'









share|improve this question

























  • please show us the first lines of tf.sh

    – Yaron
    Jan 14 at 8:45











  • @Yaron oooo I did

    – Born vs. Me
    Jan 14 at 8:49






  • 5





    Possible duplicate of How to run an alias in a shell script?

    – RoVo
    Jan 14 at 8:55











  • You need to source the .bashrc in your script and make it interactive with set -i.

    – RoVo
    Jan 14 at 8:57











  • How rovovovo? Show the full command please

    – Born vs. Me
    Jan 14 at 9:03
















0















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'









share|improve this question

























  • please show us the first lines of tf.sh

    – Yaron
    Jan 14 at 8:45











  • @Yaron oooo I did

    – Born vs. Me
    Jan 14 at 8:49






  • 5





    Possible duplicate of How to run an alias in a shell script?

    – RoVo
    Jan 14 at 8:55











  • You need to source the .bashrc in your script and make it interactive with set -i.

    – RoVo
    Jan 14 at 8:57











  • How rovovovo? Show the full command please

    – Born vs. Me
    Jan 14 at 9:03














0












0








0








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'









share|improve this question
















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'






14.04 bash scripts alias






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 14 at 10:02









karel

58.9k13128148




58.9k13128148










asked Jan 14 at 8:42









Born vs. MeBorn vs. Me

115




115













  • please show us the first lines of tf.sh

    – Yaron
    Jan 14 at 8:45











  • @Yaron oooo I did

    – Born vs. Me
    Jan 14 at 8:49






  • 5





    Possible duplicate of How to run an alias in a shell script?

    – RoVo
    Jan 14 at 8:55











  • You need to source the .bashrc in your script and make it interactive with set -i.

    – RoVo
    Jan 14 at 8:57











  • How rovovovo? Show the full command please

    – Born vs. Me
    Jan 14 at 9:03



















  • please show us the first lines of tf.sh

    – Yaron
    Jan 14 at 8:45











  • @Yaron oooo I did

    – Born vs. Me
    Jan 14 at 8:49






  • 5





    Possible duplicate of How to run an alias in a shell script?

    – RoVo
    Jan 14 at 8:55











  • You need to source the .bashrc in your script and make it interactive with set -i.

    – RoVo
    Jan 14 at 8:57











  • How rovovovo? Show the full command please

    – Born vs. Me
    Jan 14 at 9:03

















please show us the first lines of tf.sh

– Yaron
Jan 14 at 8:45





please show us the first lines of tf.sh

– Yaron
Jan 14 at 8:45













@Yaron oooo I did

– Born vs. Me
Jan 14 at 8:49





@Yaron oooo I did

– Born vs. Me
Jan 14 at 8:49




5




5





Possible duplicate of How to run an alias in a shell script?

– RoVo
Jan 14 at 8:55





Possible duplicate of How to run an alias in a shell script?

– RoVo
Jan 14 at 8:55













You need to source the .bashrc in your script and make it interactive with set -i.

– RoVo
Jan 14 at 8:57





You need to source the .bashrc in your script and make it interactive with set -i.

– RoVo
Jan 14 at 8:57













How rovovovo? Show the full command please

– Born vs. Me
Jan 14 at 9:03





How rovovovo? Show the full command please

– Born vs. Me
Jan 14 at 9:03










2 Answers
2






active

oldest

votes


















0














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





share|improve this answer
























  • Just did that, didnt work

    – Born vs. Me
    Jan 14 at 9:01



















0














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|improve this answer


























  • The alias is a python command How can I make it to run it directlu onj the scrtipt? Imn happy with that

    – Born vs. Me
    Jan 14 at 9:02











  • @Bornvs.Me updated my answer

    – Mr Shunz
    Jan 14 at 9:07











  • 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

    – Born vs. Me
    Jan 14 at 9:18











  • @Bornvs.Me sorry, there should be NO spaces before END.

    – Mr Shunz
    Jan 14 at 9:19











  • File "<stdin>", line 1 import sys, urllib as ul ^ IndentationError: unexpected indent Python problem now :)

    – Born vs. Me
    Jan 14 at 9:29











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1109564%2falias-not-working-inside-bash-shell-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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





share|improve this answer
























  • Just did that, didnt work

    – Born vs. Me
    Jan 14 at 9:01
















0














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





share|improve this answer
























  • Just did that, didnt work

    – Born vs. Me
    Jan 14 at 9:01














0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 14 at 8:55









YaronYaron

9,05871940




9,05871940













  • Just did that, didnt work

    – Born vs. Me
    Jan 14 at 9:01



















  • Just did that, didnt work

    – Born vs. Me
    Jan 14 at 9:01

















Just did that, didnt work

– Born vs. Me
Jan 14 at 9:01





Just did that, didnt work

– Born vs. Me
Jan 14 at 9:01













0














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|improve this answer


























  • The alias is a python command How can I make it to run it directlu onj the scrtipt? Imn happy with that

    – Born vs. Me
    Jan 14 at 9:02











  • @Bornvs.Me updated my answer

    – Mr Shunz
    Jan 14 at 9:07











  • 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

    – Born vs. Me
    Jan 14 at 9:18











  • @Bornvs.Me sorry, there should be NO spaces before END.

    – Mr Shunz
    Jan 14 at 9:19











  • File "<stdin>", line 1 import sys, urllib as ul ^ IndentationError: unexpected indent Python problem now :)

    – Born vs. Me
    Jan 14 at 9:29
















0














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|improve this answer


























  • The alias is a python command How can I make it to run it directlu onj the scrtipt? Imn happy with that

    – Born vs. Me
    Jan 14 at 9:02











  • @Bornvs.Me updated my answer

    – Mr Shunz
    Jan 14 at 9:07











  • 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

    – Born vs. Me
    Jan 14 at 9:18











  • @Bornvs.Me sorry, there should be NO spaces before END.

    – Mr Shunz
    Jan 14 at 9:19











  • File "<stdin>", line 1 import sys, urllib as ul ^ IndentationError: unexpected indent Python problem now :)

    – Born vs. Me
    Jan 14 at 9:29














0












0








0







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|improve this answer















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|improve this answer














share|improve this answer



share|improve this answer








edited Jan 14 at 9:36

























answered Jan 14 at 8:59









Mr ShunzMr Shunz

2,41121922




2,41121922













  • The alias is a python command How can I make it to run it directlu onj the scrtipt? Imn happy with that

    – Born vs. Me
    Jan 14 at 9:02











  • @Bornvs.Me updated my answer

    – Mr Shunz
    Jan 14 at 9:07











  • 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

    – Born vs. Me
    Jan 14 at 9:18











  • @Bornvs.Me sorry, there should be NO spaces before END.

    – Mr Shunz
    Jan 14 at 9:19











  • File "<stdin>", line 1 import sys, urllib as ul ^ IndentationError: unexpected indent Python problem now :)

    – Born vs. Me
    Jan 14 at 9:29



















  • The alias is a python command How can I make it to run it directlu onj the scrtipt? Imn happy with that

    – Born vs. Me
    Jan 14 at 9:02











  • @Bornvs.Me updated my answer

    – Mr Shunz
    Jan 14 at 9:07











  • 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

    – Born vs. Me
    Jan 14 at 9:18











  • @Bornvs.Me sorry, there should be NO spaces before END.

    – Mr Shunz
    Jan 14 at 9:19











  • File "<stdin>", line 1 import sys, urllib as ul ^ IndentationError: unexpected indent Python problem now :)

    – Born vs. Me
    Jan 14 at 9:29

















The alias is a python command How can I make it to run it directlu onj the scrtipt? Imn happy with that

– Born vs. Me
Jan 14 at 9:02





The alias is a python command How can I make it to run it directlu onj the scrtipt? Imn happy with that

– Born vs. Me
Jan 14 at 9:02













@Bornvs.Me updated my answer

– Mr Shunz
Jan 14 at 9:07





@Bornvs.Me updated my answer

– Mr Shunz
Jan 14 at 9:07













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

– Born vs. Me
Jan 14 at 9:18





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

– Born vs. Me
Jan 14 at 9:18













@Bornvs.Me sorry, there should be NO spaces before END.

– Mr Shunz
Jan 14 at 9:19





@Bornvs.Me sorry, there should be NO spaces before END.

– Mr Shunz
Jan 14 at 9:19













File "<stdin>", line 1 import sys, urllib as ul ^ IndentationError: unexpected indent Python problem now :)

– Born vs. Me
Jan 14 at 9:29





File "<stdin>", line 1 import sys, urllib as ul ^ IndentationError: unexpected indent Python problem now :)

– Born vs. Me
Jan 14 at 9:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1109564%2falias-not-working-inside-bash-shell-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Human spaceflight

Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

File:DeusFollowingSea.jpg