1

I have an alias like alias cdpy="cd python" in my .bash_profile and I have sourced it. But I am still not able to use that in another shell script of mine, which is called pygitup.

I googled it and got some answers like adding shopt -s expand_aliases. I have added it to the pygitup but it still doesn't work. Am I using it wrong? This is how I use it:

# some code
shopt -s expand_aliases
cdpy
# some code
4
  • You'd have to source it in pygitup.sh as well. Commented Jan 10, 2019 at 19:10
  • @thatotherguy Thanks. But it only works when I call "source pygitup", which means if I just do pygitup "something", I still get command not found. Should I call source pygitup "something" every time instead of merely pygitup "something"? Is there a way to simplify the "source"? Commented Jan 10, 2019 at 19:19
  • I meant that you have to add source ~/.bash_profile in your pygitup script. Commented Jan 10, 2019 at 19:20
  • @thatotherguy Thank you! Problem resolved. Do you mind turning your comment into an answer? And I added the source ~/.bash_profile right before the shopt line. Although it works I still wonder did I add it to the right place? Commented Jan 10, 2019 at 19:24

2 Answers 2

2

If you run pygitup from your environment with a leading "dot space", it will inherit your shell's configuration, including aliases.

A simple example with a bash script:

user@pc:~ $ alias e='echo alias is set'
user@pc:~ $ e
alias is set
user@pc:~ $ vim pygitup.sh
user@pc:~ $ cat pygitup.sh 
#!/bin/bash
e
user@pc:~ $
user@pc:~ $ ./pygitup.sh 
./pygitup.sh: line 2: e: command not found
user@pc:~ $ 
user@pc:~ $ 
user@pc:~ $ . ./pygitup.sh       # <--- notice the leading dot
alias is set
user@pc:~ $ 
Sign up to request clarification or add additional context in comments.

1 Comment

You seem to use the alias in the terminal. Does it still work if the alias is in another file?
1

Aliases are not inherited by external commands, so there's no alias to expand. You'd have to source the file that defines the alias again in your script:

# some code
shopt -s expand_aliases
source ~/.bash_profile
cdpy
# some code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.