1

According to bash man pages and other online sources, the source command and dot should behave in exactly the same way when sourcing an existing script, but that is not what I observe. Consider this following script which echoes a statement indicating whether a variable is set.

#! /bin/bash
if [[ -v MY_VAR ]]; then
 echo "source-test.bash already sourced"
 return
fi
export MY_VAR=YES

Here are the results of executing it.

echo $MY_VAR
source source-test.bash
source source-test.bash
source-test.bash already sourced <---- echoed from the script as expected
. source-test.bash
/home/foobar/bin <--- Why is it printing the current directory instead of echoing the output message?

Expected Result After the first time the script is sourced, I expected the string "source-test.bash already sourced" to be echoed regardless of whether I use source or the dot or source command to source the script. Why is the home directory being echoed when I use the dot instead of source?

asked Feb 21, 2023 at 22:46
2
  • It works for me unless I declare a function named .: .() { echo ~ ; }. Commented Feb 21, 2023 at 22:53
  • You're both right. How interesting. Evidently, I copied some things from a coworkers profile with some useful aliases and the period was aliased to pwd. I never noticed that subtle alias definition, and I used to always use source until I learned that the dot is supposed to work the same way so I had never noticed until now. Commented Feb 21, 2023 at 23:00

1 Answer 1

2

Evidently, I copied some things from a coworker's bash profile with some useful aliases and the period was aliased to pwd. I never noticed that subtle alias definition. In the past I've used the source command, and only after I attempted to use the dot instead of the source command did I realize that this alias existed.

In the .bashrc there was

alias .='pwd'

One of the commenters suggested that I run the following command to determine if the . was aliased to something. This is a good trick that will work in other situations when a command isn't working as you'd expect. Sometimes when an alias is created you might use a combination of letters or a letter that you didn't realize at the time conflicted with an actual command or program name.

type -a .

This is what led me to solve the issue by removing the alias from the .bashrc file.

Kamil Maciorowski
83k25 gold badges169 silver badges264 bronze badges
answered Feb 22, 2023 at 16:55

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.