简体   繁体   中英

Sourcing .bashrc in Travis-CI not working

The last couple of days I've been trying to setup a Travis-CI build for my bash scripting project. I am having a problem with sticking an alias into the .bashrc that lives in the Travis build and not sourcing.

Below is my simple example of creating a bash alias in the .bashrc file on Linux, and the attempt failing.

Travis-CI (.travis.yaml):

language: bash

git:
  quiet: true
  submodules: false

matrix:
  include:
    - os: linux
      dist: xenial

script:
  - sh test_bash.sh || travis_terminate 1;
  - bash test_sourcing.sh || travis_terminate 1;

test_bash.sh:

current_shell=$(echo $SHELL)
if [ "$current_shell" != "/bin/bash" ]; then
    echo "The current build is not working with the Bash Shell."
    exit 1
fi 

test_sourcing.sh

alias name='echo "John Doe"' >> $HOME/.bashrc
source $HOME/.bashrc
output=$(name)
if [ "$output" != "John Doe" ]; then
    echo "Sourcing is not working for some reason."
    exit 1
fi 

What I get from the output of my build is the following:

$ bash -c 'echo $BASH_VERSION'
3.2.57(1)-release
0.02s$ sh test_bash.sh || travis_terminate 1;
The command "sh test_bash.sh || travis_terminate 1;" exited with 0.
$ bash test_sourcing.sh || travis_terminate 1;
test_sourcing.sh: line 3: name: command not found
Sourcing is not working for some reason.

I expected to get all tests to pass but I am having a hard time understanding such a simple feature. The only thing I can think of is that the version of BASH is at a version that alias is not supported. Thanks for any help!

you could work with simple variables or execute them through eval , which is simlar to what you want.

Look for name2 and the two options with and without eval . name1 is your code.

$ cat test_sourcing.sh
set -x
alias name1='echo "John Doe"' >> $HOME/.bashrc
name2='echo "John Doe"' >> $HOME/.bashrc
source $HOME/.bashrc
output=$($name1)
output=$($name2)
output=$(eval $name2)
if [ "$output" != "John Doe" ]; then
    echo "Sourcing is not working for some reason."
    exit 1
fi

when running the script you can see:

$ ./test_sourcing.sh
++ alias 'name1=echo "John Doe"'
++ name2='echo "John Doe"'
++ source /home/schroen/.bashrc
+++ case $- in
+++ return
++ output=
+++ echo '"John' 'Doe"'
++ output='"John Doe"'
+++ eval echo '"John' 'Doe"'
++++ echo 'John Doe'
++ output='John Doe'
++ '[' 'John Doe' '!=' 'John Doe' ']'
  • 1st output is empty, because alias is not expanding (like chepner said)
  • 2nd output is set to "John Doe" but echo is run directly. (mind the " inside the variable value)
  • 3rd output is set to "John Doe" too, but via eval which runs the echo. Sometimes this is important when building variablenames with other variable.

this eval thing...

$ cat variable-loop.sh
#!/usr/bin/env bash
#set -x

dev1=foo
dev2=bar
dev3=foobar

echo 'without eval not what we want...'
for i in $(seq 1 3); do
        echo dev$i
        echo $dev$i
done

echo 'with eval it is working...'
for i in $(seq 1 3); do
        eval echo \$dev$i
done

$ ./variable-loop.sh 
without eval not what we want...
dev1
1
dev2
2
dev3
3
with eval it is working...
foo
bar
foobar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM