LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash scripting issue with "source" or . (dot) operator (Cygwin & Ubuntu) (https://www.linuxquestions.org/questions/programming-9/bash-scripting-issue-with-source-or-dot-operator-cygwin-and-ubuntu-784018/)

brian.hussey 01-22-2010 08:48 AM

Bash scripting issue with "source" or . (dot) operator (Cygwin & Ubuntu)
 
Hi All,
This one is driving me crazy.

My .bashrc is set as:
if [ -d ~/.bashrc.d ]; then
for file in $(/bin/ls ~/.bashrc.d/); do
. '~/.bashrc.d/'$file
done
fi

So in theory any files located in the ~/.bashrc.d/ directory will automatically get loaded.
great!

The problem is that when running the script, the file (that it has already found) that it tries to load gives an error like:

bash: ~/.bashrc.d/aliases: No such file or directory

I've debugged and debugged but the best I've come up with is the line doing the source (the . ) is:
. ~/.bashrc.d/aliases
(the file does exist btw)

if I run the same command from the shell it works fine and all my aliases load correctly.

Can anyone help me please?

I'm getting this in Cygwin and Ubuntu.

Thanks,
Brian

brian.hussey 01-22-2010 09:28 AM

Hi All,
I figured it out:
. '~/.bashrc.d/'$file
should have been:
. ~/.bashrc.d/$file

Then it all works correctly.

I hope this helps someone, someday.

Brian

tuxdev 01-22-2010 09:49 AM

You are doing two things wrong here:
1. Trying to do anything besides display the output of "ls"
2. Using single-quotes instead of double-quotes
Code:

shopt -s nullglob
for file in "$HOME/.bashrc.d"/*; do
  source "$file"
done
shopt -u nullglob


brian.hussey 01-22-2010 01:11 PM

Thanks very much!


All times are GMT -5. The time now is 01:52 PM.