How to know which file sourced a particular file - linux

Ubuntu 16.04; zsh; oh my zsh; KDE
I have many dotfiles like .bashrc, .commonrc, .zshrc sourcing each other. Say I want to stop sourcing a particular file .bash_docker. Is there a way to know which file sourced it?
I figured it out through manual examination, but it happens quite often... It seems like source has no man page or help option.

Try printing this in your target script
echo "This script sourced from : ${BASH_SOURCE[1]}"
Here are the bash variables of interest for reference
https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html

Related

Sourcing from a different bashrc

My original .bashrc script is currently used to run model runs. Now I need to manipulate it to compile a completely new model.
My question is, if I save my original .bashrc, as something such as .bwwbashrc, do I need to manipulate the file in some way so it is able to be read or recognized as the .bashrc when I source it within my scripts?
original sourcing
source /home/tsee/.bashrc
What I think the new sourcing would be.
(after creating .bwwbashrc)
source /home/tsee/.bwwbashrc
Just not sure if I need to save it with a certain extension or edit the executable in some sort of way.
Nope, you can name it whatever you want. Executable bit isn't required either.
If you aren't aware of it, the bash --login option might be of interest to you.
To complement Matt’s correct answer, I’d also point out that you can start a new Bash shell that sources your alternative file instead of .bashrc at start-up.
bash --rcfile .bwwbashrc
From the bash man page:
--rcfile file
Execute commands from file instead of the standard personal initialization
file ~/.bashrc if the shell is interactive (see INVOCATION below).
If you want to replace your current shell (with commands and settings from .bashrc), you can run
exec bash --rcfile .bwwbashrc

Setting path variables and running Ruby script

This is my first time working with a Ruby script, and, in order to run this script, I have to first cd into the root of the project, which is /usr/local/bin/youtube-multiple-dl and then execute the script as bin/youtube-multiple-dl.
I tried setting the PATH variable
echo 'export PATH="$HOME/youtube-multiple-dl/bin:$PATH"' >> ~/.bash_profile
in hopes that I can run this from anywhere on the machine without having to cd to the project's root, however, no luck with that so far.
System: Ubuntu 15.04 server
Script Repo
My current way of executing the script is:
root#box15990:~# cd /usr/local/bin/youtube-multiple-dl
root#box15990:/usr/local/bin/youtube-multiple-dl# bin/youtube-multiple-dl
Desired way of executing script:
root#box15990:~# youtube-multiple-dl
How can I properly set the enviroment path for this script in order to run from anywhere?
echo 'export PATH="$HOME/youtube-multiple-dl/bin:$PATH"' >> ~/.bash_profile
isn't how we set a PATH entry.
The PATH is a list of directories to be searched, not a list of files.
Typically, the PATH should contain something like:
/usr/local/bin:/usr/bin
somewhere in it.
If it doesn't, then you want to modify it using a text editor, such as nano, pico or vim using one of these commands:
nano ~/.bash_profile
pico ~/.bash_profile
vim ~/.bash_profile
You probably want one of the first two over vim as vim, while being extremely powerful and one of the most-used editors in the world, is also not overly intuitive if you're not used to it. You can use man nano or man pico to learn about the other too.
Once your in your file editor, scroll to the bottom and remove the line you added. Then find the /usr/bin section in your PATH and add /usr/local/bin: before it. : is the delimiter between directories. That change will tell the shell to look in /usr/local/bin before /usr/bin, so that any things you added to the /usr/local/bin directory will be found before the system-installed code, which is in /usr/bin.
It's possible that there isn't a PATH statement in the file. If you don't see one, simply add:
export PATH=/usr/local/bin:$PATH
After modifying your ~/.bash_profile, save the file and exit the editor, and then restart your shell. You can do that by exiting and re-opening a terminal window, or by running:
exec $SHELL
at the command-line.
At that point, running:
echo $PATH
should reflect the change to your path.
To confirm that the change is in effect, you can run:
which youtube-multiple.dl
and you should get back:
/usr/local/bin/youtube-multiple.dl
At that point you should be able to run:
youtube-multiple.dl -h
and get back a response showing the built-in help. This is because the shell will search the path, starting with the first defined directory, and continue until it exhausts the list, and will execute the first file matching that name.
Because of the difficulties you're having, I'd strongly recommend reading some tutorials about managing a *nix system. It's not overly hard to learn the basics, and having an understanding of how the shell finds files and executes them is essential for anyone programming a scripting language like Ruby, Python, Perl, etc. We're using the OS constantly, installing files for system and user's use, and doing so correctly and safely is very important for the security and stability of the machine.

Need to type exec bash everytime at startup

I'm sure this is a fairly simple problem, but I've put together a .bashrc file (located in my home directory) It includes my PS settings, some environmental exports and some aliases. First, I've read that these aren't all supposed to go into a .bashrc file but instead to include some of it in .bash_profile
If I include a .bash_profile file then none of the changes make any effect. If I keep everything as one long .bashrc file then everything works as planned....once "exec bash" is entered at the command line. Every time I start my unix environment however, I have to type "exec bash" in order for everything to work. Am I just missing something really simple here? Sorry if this has been answered, a search didn't really bring up anything useful (could be my poor search!)
According to [Man7]: chsh(1) (or man chsh), you should run:
chsh -s /bin/bash
Note: On some Nix distributions, location might be: /usr/bin/bash.

How to add in bash for auto completion of arcanist commands

I am new to linux.
I am trying to set up arcanist.
I am done with git clone and adding the path in environment variable, however I am confused on how to set up the tab completion for arcanist commands.
In the arcanist user guide it says that you need to add source /path/to/arcanist/resources/shell/bash-completion to your .bashrc, .profile files.
What are these files and how can I edit them to work with arcanist with the tab completion.
The .bashrc, found in your home directory, is the configuration file for that user for bash.
There is a global bashrc usually located in /etc/bashrc.
The difference to .profile is that the .bashrc is executed every time you start a terminal (bash) while .profile only once when you use a login shell.
The command source loads everything that is inside the file you use with that command and treats it as if you wrote those commands in the .bashrc yourself. I guess in /path/to/arcanist/resources/shell/bash-completion are aliases/functions/etc. which enable tab-completion with arcanist.
Edit: for bash, the profile file is usually called .bash_profile.
After lot of googling and asking people, I finally did it.
First of all I had to export the path where my arcanist code from the github has been cloned in the ~/.bashsrc file(in bold below)
export PATH="$PATH:$HOME/.rvm/bin:$HOME/arcanist/bin/"
After this command, I copied the function which was present in the /arcanist/resources/shell/bash-completion into the bashrc file
And then I closed and open the terminal and bingo I was able to get arc and it's command as auto completion on striking tab.
Thanks ap0 for the comments.

How to make vim use the same environment as my login shell when running commands with "!"?

I use !ls to execute bash command. But after i have configured something like source ~/.usr_profile in ~/.profile file, vim won't source this file as well. So when i want to execute a function declared in usr_profile , i have to run :!source ~/.usr_profile && my_command. When i using this once or twice, it's ok. But when use it frequently, the my vimrc becomes messy.
Is there any better method to solve this problem.Thanks
Adding this line to your ~/.vimrc should solve your immediate problem:
set shell=bash\ -l
When invoked with -l (--login), bash reads your ~/.profile at startup (among other files) and thus everything sourced from there.
When invoked with -i (--interactive), bash reads your ~/.bashrc at startup (among other files) and thus everything sourced from there.
Try $ man bash or :h shell and :h shellcmdflag for more info.
As for the differences between login and non-login shell I must admit my ignorance. This answer on serverfault may help, it's interesting, anyway.
Append the following two lines to your .vimrc.
set shell=/bin/bash " use bash
set shellcmdflag="-ic" " flag passed to the shell to execute "!" and ":!" commands
P.S. It seems that the accepted answer may cause some problems when editing '.bash' files or execute a 'git commit', but I don't know why.

Resources