1

Take the following script:

shopt -s expand_aliases
set -f
result=$(compgen -A function)
echo $result

When running it outputs all my custom bash functions:

mp3gain pkg-pkgbuild-download quote quote_readline restart standby turnoff turnoff-timer youtubeConvert

However, when slightly changing the script to output aliases, the output is empty:

shopt -s expand_aliases
set -f
result=$(compgen -A alias)
echo $result

Yet it is not empty if I run compgen -A alias directly.

My aliases are stored in ~/.bash_aliases and my functions in /.bash_functions. Both are sources in ~/.bashrc:

# Functions
if [ -f ~/.bash_functions ]; then
        . ~/.bash_functions
fi

# Aliases
if [ -f ~/.bash_aliases ]; then
        . ~/.bash_aliases
fi

What am I missing here?

3
  • This answer should explain the problem. Commented Dec 5, 2013 at 7:15
  • You are totally right. I forgot to mention that I am sourcing my aliases and functions. I edited my first post. Commented Dec 5, 2013 at 7:22
  • Source those files in your script. Commented Dec 5, 2013 at 7:26

1 Answer 1

1

I'd be willing to wager that the aliases are not sourced. This can be verified running this simple script:

#!/bin/bash
alias

If there is no output, the aliases are not sourced. Hence, that's why compgen returns an empty list when put in a script (non-sourced aliases) but works fine when run manually in a shell with sourced aliases.

Solution: put "source ~/.bash_aliases" near the top of your script to make sure they are invoked before running.

Sign up to request clarification or add additional context in comments.

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.