linux: how to execute profile file [duplicate] - linux

This question already has answers here:
How to reload .bashrc settings without logging out and back in again?
(18 answers)
Closed 3 years ago.
My colleague gave me a file containing lots of configs such as
alias ll="ls -l"
alias lr="ls -lrt"
alias gv="vim -g"
How can I use(execute) this profile?

You can load the profile using source command:
source <profile-filename>
eg:
source ~/.bash_profile

For your custom aliases, the best place should be at ~/.bash_aliases. You must just be sure the ~/.bashrc file already contains the following lines:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
or in a more concise manner:
[ -f ~/.bash_aliases ] && . ~/.bash_aliases
To load them immediately, source it. Otherwise, aliases will be loaded at every terminal opening. To check, use the alias command without argument.

You can put it in your local .bashrc (or appropriate shell rc file) file if you want it permanently.
cat fileNameProfile >> ~/.bashrc
And for current session:
source ~/.bashrc
If you want it just now, then:
source fileNameProfile

For one time use, copy paste commands to your terminal:-
alias ll="ls -l"
alias lr="ls -lrt"
alias gv="vim -g"
For everytime use, add it in .bashrc.
echo 'alias ll="ls -l"' >> ~/.bashrc
echo 'alias lr="ls -lrt"' >> ~/.bashrc
echo 'alias gv="vim -g"' >> ~/.bashrc
and then source ~/.bashrc

To autoload these alias commands, create a .sample_profile file and add the alias on the file.
after that add this text in .bashrc file
.bashrc
if [ -f ~/.sample_profile ]; then
. ~/.sample_profile
fi

Related

linux alias to simplify a command line

I am new to writing Linux scripts (in fact I'm not sure if the proper term is Linux script or baSH script). I do understand source-ing and the alias feature such as this:
alias l='ls -ltra'
What I want to accomplish is type the following shortcut statement
php ~/path/to/longProgram.php Argument1 -x -y -z --long-switch long-switch-value
as this:
lp Argument1 -x -y -z --long-switch long-switch-value
i.e. where I can call lp from any where, and where all of the arguments as-given get passed to longProgram.php. How would I do this as an alias?
To make an invocation of lp Argument1 -x -y -z --long-switch long-switch-value result in a call to php ~/path/to/longProgram.php Argument1 -x -y -z --long-switch long-switch-value, you have a few options. The simplest is to put this in your ~/.bashrc:
lp() { php "$HOME/path/to/longProgram.php" "$#"; }
alias lp='php ~/path/to/longProgram.php'
would normally work, even if it is not a good practice. An alias is simply a find and replace feature so I see no reason of this not working. However you need to be the exact user whose home contain the program.
PS: Also verify that lp is not already assigned to another command.
I just found a way to set an alias command and save it for next time.
nano ~/.bashrc or ~/.bash_aliases
add your aliases in this file
Ex:
alias lp='php ~/path/to/longProgram.php
ctrl+x to save the file, then
source ~/.bashrc or ~/.bash_aliases
You will be able to use the alias command everytime you login as the target user.
Note:
If you want to use ~/.bash_aliases, be sure that the ~/.bashrc file has following commands.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
I prefer using bash_aliases to keep your custom alias clean and management in a centrilized file.
Referece:
https://www.cyberciti.biz/faq/create-permanent-bash-alias-linux-unix/

Bash script to go to directory and perform commands gives "Command not found" [duplicate]

This question already has answers here:
How to use aliases defined in .bashrc in other scripts?
(6 answers)
Closed 2 years ago.
My alias defined in a sample shell script is not working. And I am new to Linux Shell Scripting.
Below is the sample shell file
#!/bin/sh
echo "Setting Sample aliases ..."
alias xyz="cd /home/usr/src/xyz"
echo "Setting done ..."
On executing this script, I can see the echo messages. But if I execute the alias command, I see the below error
xyz: command not found
am I missing something ?
source your script, don't execute it like ./foo.sh or sh foo.sh
If you execute your script like that, it is running in sub-shell, not your current.
source foo.sh
would work for you.
You need to set a specific option to do so, expand_aliases:
shopt -s expand_aliases
Example:
# With option
$ cat a
#!/bin/bash
shopt -s expand_aliases
alias a="echo b"
type a
a
$ ./a
# a is aliased to 'echo b'
b
# Without option
$ cat a
#!/bin/bash
alias a="echo b"
type a
a
$ ./a
./a: line 3: type: a: not found
./a: line 4: a: command not found
reference: https://unix.stackexchange.com/a/1498/27031 and https://askubuntu.com/a/98786/127746
sourcing the script source script.sh
./script.sh will be executed in a sub-shell and the changes made apply only the to sub-shell. Once the command terminates, the sub-shell goes and so do the changes.
OR
HACK: Simply run following command on shell and then execute the script.
alias xyz="cd /home/usr/src/xyz"
./script.sh
To unalias use following on shell prompt
unalias xyz
If you execute it in a script, the alias will be over by the time the script finishes executing.
In case you want it to be permanent:
Your alias is well defined, but you have to store it in ~/.bashrc, not in a shell script.
Add it to that file and then source it with . .bashrc - it will load the file so that alias will be possible to use.
In case you want it to be used just in current session:
Just write it in your console prompt.
$ aa
The program 'aa' is currently not installed. ...
$
$ alias aa="echo hello"
$
$ aa
hello
$
Also: From Kent answer we can see that you can also source it by source your_file. In that case you do not need to use a shell script, just a normal file will make it.
You may use the below command.
shopt -s expand_aliases
source ~/.bashrc
eval $command
Your alias has to be in your .profile file not in your script if you are calling it on the prompt.
If you put an alias in your script then you have to call it within your script.
Source the file is the correct answer when trying to run a script that inside has an alias.
source yourscript.sh
Put your alias in a file call ~/.bash_aliases and then, on many distributions, it will get loaded automatically, no need to manually run the source command to load it.

Cannot add environment variables in linux

For an entire day I have been tryng to add an environment variable to linux ad it isn't working. This is the guide that I am following. This is what is written in the .profile file.
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
JAVA_HOME=/usr/local/java/jdk1.8.0_51
JRE_HOME=$JAVA_HOME/jre
LARAVEL=/home/user/.composer/vendor/bin
PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin:$LARAVEL
export JAVA_HOME
export JRE_HOME
export LARAVEL
export PATH
I am running the file. But when I run echo $PATH I am not getting the $LARAVEL path. What am I doing wrong?
Have you logged out and logged in? Also try to update your .bashrc file instead. I think that .profile is only used for the ksh shell.
Try to source the file, like;
$ source yourfilename
In Unix/Linux (unlike in Windows) a program cannot affect the environment of the parent process. When you are running it as a regular shellscript it becomes a subprocess of the parent, and it will affect only it's own environment and not that of the parent.
Using the keyword source (or using the dot .) will instruct your process to executed the shell script directly rather than fork a new subprocess.
echo 'export PATH="$PATH":/path/to/folder/' >> ~/.bashrc
and
source ~/.bashrc

mac how to save alias in computer

I created an alias on terminal, such as
alias hw="cd Desktop/2015hw"
but after I closed the terminal and do hw, there is an error message said there is no hw command.
I was wondering if we can save the alias on the computer.
Also, if we have stored the alias on the computer, how would we check it? By check it, I mean like list all alias we have stored.
Thanks.
Add the command to your .bashrc file
echo "alias hw='cd Desktop/2015hw'" >> ~/.bashrc
Keep in mind that your alias will only work when you are in your home (as you are using a relative path)
echo "alias hw='cd ~/Desktop/2015hw'" >> ~/.bashrc
Execute the saved alias by sourcing the file
source ~/.bashrc
When making an alias with alias, the alias is only valid until you close the terminal window. If you open another terminal window, the alias will no longer be present.
You can make an alias be valid for all terminal windows by placing it in your ~/.bash_profile or ~/.bashrc files (bash is the name of the terminal which Mac OS X ships with by default.)
Since Mac OS X does not load .bashrc by default, I would actually do:
echo "alias hw='cd ~/Desktop/2015hw'" >> ~/.bash_profile
When you want to delete the alias, you can open your ~/.bash_profile or ~/.bashrc files and delete it manually.
To open them, do open -a TextEdit ~/.bash_profile or open -a TextEdit ~/.bashrc
Finally, to list all your current aliases, simply type alias in Terminal.
your can start with type
ls -la
and then if you use bash typing
nano .bash_profile
or if you use zsh typing
nano .zshrc
then make alias save with ctrl + o
for activating
use
source ~/.bash_profile or
source ~/.zsh

Update .bashrc from provisioning shell script with Vagrant

I'm trying to add some additional lines to .bashrc in my home directory from the provisioning shell script when launching a new instance with Vagrant.
In the shell script I have:
set -x
sudo apt-get update
sudo apt-get install vim
echo "source /usr/local/share/chruby/chruby.sh">> ~/.bashrc
echo "source /usr/local/share/chruby/auto.sh">> ~/.bashrc
However after completion nothing has been written to .bashrc.
This is a cut down version of the full script the intention of which is to install Ruby/Rails.
You need to give the full path to the file.
E.g.
echo "source /usr/local/share/chruby/chruby.sh" >> /home/vagrant/.bashrc
Add these lines to .bashrc
if [ -f /usr/local/share/chruby/chruby.sh ]; then
. /usr/local/share/chruby/chruby.sh
fi
It will textually include the script into .bashrc and execute it when opening a new shell.
Try this for your last 2 lines - it should give you exactly what you need.
echo "source /usr/local/share/chruby/chruby.sh" >> /home/vagrant/.bashrc
echo "source /usr/local/share/chruby/auto.sh" >> /home/vagrant/.bashrc

Resources