I have a .bashrc file that needs to be sourced non-interactively. The problem is that on the file there is this return statement preventing from sourcing by code:

[ -z "$PS1" ] && return 

To overcome this without modifying the remote .bashrc file I cat the file, grep out this line and source it as below:

user@host -t "
  source <( echo \"\$(cat /home/release/.bashrc | grep -v \"return\")\" ); 
  alias v; 

P.S: Had I sourced .bash_profile the results would be the same as if I hadn't sourced it at all.

What I find strange is that if I declare an alias like so:

alias v='cat somefile'

and then run these two commands as previously:

alias v;

I get the following output for each:

alias v='cat somefile'
bash: v: command not found

My question is: If it can see that there is an alias defined, why it won't recognize the command?


From the bash man page

Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt

when you run a shell script, as you know, it spawns another subshell, runs your script in that shell and returns the output to the current shell. So, the script is running in a non-interactive shell.

Although I wouldn't advise, you can change this behavior, by using the following command:

 shopt -s expand_aliases
 alias v='cat somefile'

and you should see it working. The advice against using it comes from people get lazy and take things for granted in time and forget what they did. And god forbid if you need to give the reins of your systems to new sysadmin when you move to greener pastures. Probably the poor soul will have no clue what you did and shoot himself or herself in the foot. My advice is sticking with simple and widely known/accepted operation principles instead of wildly customized environments.

  • I was previously trying to expand aliases from my local terminal, it wasn't working, it only worked after shopt on my local bashrc. Thanks for the advice, we actually have a release user with its own configuration and each of us here have our own user configured on the development node, as well. The releasing steps remains the same for everyone, I just minimised my commands to a single function on my profile. I guess I need to rethink this strategy, though. – Alan Mar 3 '16 at 16:26
  • Also, I would upvote your answer but I don't have my 15s here, yet. – Alan Mar 3 '16 at 16:28
  • I'm glad it worked for you. Don't wrry about the points. Marking it as a solution will help future users with the same or similar problems find the solution easier. – MelBurslan Mar 3 '16 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.