run a command on the remote machine after ssh login - linux

I would like to switch to the bash environment after ssh login. Currently I have to type bash every time and then cd to my working directory.
Where can I add some settings so it will run automatically for me.

There's an environment variable SHELL that is set to the current shell. You can set it to your preferred shell by running
$ export SHELL=/path/to/shell
In order to find the path to your preferred shell just run following command
$ whereis bash
Enter the path returned by whereis command as your shell path.
There's a .profile(ls -al) or bash_profile that you can add your setting there. If you can't find such a file then create one using touch .profile. (I did create this file on OS X.)
Open the .profile in order to edit it using whatever text editors that you want
$ vim .profile
Then change the $HOME environment variable in order to change your default home directory path. Enter below line in opened file
export HOME=/your/path
Save the file
:w

Related

creating a command to go to a specific shortcut

So basically, I just installed ubuntu on bash on my windows PC and every time I have to go to desktop I have to type cd /mnt/c/Users/Name/Desktop. is there a way that I can make a script or command so everytime i type in "desktop" it changes my directory to desktop? I've never done Linux/bash scripting and therefore I have no clue. I just use it for the g++ compiler.
There are many ways to do this:
Create an executable program that runs the command cd /mnt/c/Users/Name/Desktop (this is iain's answer, and a perfectly fine one; the only issue might be that you'll need to either enter the full path for that program every time you run it, or you'll need to put it into a directory that is already included in your $PATH environment variable.)
Create an environment variable that contains the path you'd like, then cd to it cd $DESKTOP.
export DESKTOP="/mnt/c/Users/Name/Desktop"
cd $DESKTOP
You'll likely want to put the creation of this environment variable into your .bashrc or .profile, so it gets created each time you login.
echo 'export DESKTOP="/mnt/c/Users/Name/Desktop"' >> ~/.bashrc
(Note: the above adds it to the end of the .bashrc file. Once you learn more about bash and .bashrc you will probably want to move it to another location in the file.)
Create an alias that does the same thing.
alias mydesk='cd /mnt/c/Users/Name/Desktop'
Again, you'll likely want to add the creation of this alias to your .bashrc or .profile file, so it gets created each time you login:
echo "alias mydesk='cd /mnt/c/Users/Name/Desktop'" >> ~/.bashrc
(Note: the above adds it to the end of the .bashrc file. Once you learn more about bash and .bashrc you will probably want to move it to another location in the file or even a different file completely.)
Assuming that "/mnt/c/Users/Name/" is your home directory, you can just use the shortcut for that, then append "Desktop" to it:
cd ~/Desktop
or
cd $HOME/Desktop
You can give this a try ...
make a file called desktop
Put this into it:
#!/bin/bash
cd /mnt/c/Users/Name/Desktop
close the file and then make it executable.
$ chmod +x desktop
Now by typing . desktop (note the dot) you should be taken to your desktop.
You may also be able to add the script to your path, so that it will run from anywhere in the bash environment; Depending upon how like ubuntu your environment is.

How to execute a bash script without calling its path?

I have the file script.sh in my-directory folder.
How to run this script with the command `script' from the terminal with no regards to the location I am in the terminal?
You can do so by exporting the path where your script in the PATH environment variable, so that you don't ever have to worry about what your actual script's location is, i.e. if your script is present under say /path/to/dir, do
export PATH=$PATH:/path/to/dir
so that your script's path gets appended to an already existing set if paths under PATH, also remember if you run the above from the command-line, it is not permanent and gets lost soon after the session is terminated. To make it permanent add the same line in .bashrc (or) .bash_profile, depending upon your environment.
Or creating a symbolic link from /usr/bin that is what you intent to do you can do something like ln -s /full/path/to/myscript.sh /usr/bin/myscript and then run as just myscript directly from command line. You can also confirm if is properly added by checking the script's location by which command,
$ which myscript
/usr/bin/myscript
Say your directory is /home/Cristian/my-directory then you can make that part of PATH environment variable like export PATH=$PATH:/home/Cristian/my-directory and then you will be able to call it by typing script.sh and not script. If you want it to be called as script then you should name it script and rename the extension.
The export command will make the directory in question part of PATH temporarily. To make it permanent you may want it to part of .bashrc or other shell rc file if you are in other shells.

Environment variable PATH on linux

Hi i am currently trying to set keywords for my terminal to launch some software without having to type the whole path.
For exemple:
firefox
#instead of
/home/debian/firefox/firefox
I always do this kind of thing on windows by setting path in the environment variable manager.
After i read this post PATH environment variable in linux , i added this line to the etc/environment file:
export firefox=/home/debian/firefox/firefox
#I also tried this:
export PATH=$PATH:/home/debian/firefox
It doesn't work, can someone explains me how to do this?
I would setup a new alias in my .bashrc or .profile, which should be located under your home directory. Add the following to the end of the file:
alias firefox="/home/debian/firefox/firefox"
Save the file and reload it using:
source ~/.bashrc
Since you added the alias to your .bashrc this alias will be created everytime you open a new instance of a shell.
You could use nohup to keep the command running after the shell session ends:
alias firefox="nohup /home/debian/firefox/firefox &"
Notice the trailing & character, which will run the command in the background so you can keep using your terminal.
You can also make an alias in your .bashrc file.
$ vim ~/.bashrc
It will open your .bashrc in read mode. Get in write mode by pressing i. You can create alias at anywhere in the file or below already created alias list.
alias firefox='/home/debian/firefox/firefox'
press Esc and then :wq
This will create your alias, save and exit the file. Now you only have to compile .bashrc by this
$ source ~/.bashrc
After this you can only have to use firefox instead of long /home/debian/firefox/firefox
Adding /home/debian/firefox to your PATH should have done it.
Did you start a new shell after making that change? Otherwise the new PATH would not have exported yet. Alternatively, you can just run export PATH=$PATH:/home/debian/firefox directly to update it for your current session.

Not able to access files in the bin folder after setting PATH variable

I have added HADOOP_INSTALL and it's bin to the PATH variable in my .bash_profile (shown below) and executed it using the command . .bash_profile. I can run the command hadoop version fine but when I close the terminal and run the same command again it gives me error as follows
gsidevas#gsidev-cloudvm ~]$ hadoop version
bash: hadoop: command not found
Current .bash_profile
export HADOOP_INSTALL=/usr/local/hadoop
PATH=$PATH:$HOME/bin:$HADOOP_INSTALL/bin
export PATH
What do I need to do so that this HADOOP_INSTALL and it's bin gets set permanently in my environment?
By default, BASH reads and executes commands in .bash_profile only in login shells. If you're creating a terminal via some X11 or similar software, chances are that terminal is not a login shell by default.
You can achieve this effect for every shell by simply moving the changes you made into your .bashrc file. Please note that this only works properly if your username on the system uses "bash" as its shell and not "sh" since for "sh" the .bashrc file is, by default, ignored.

what is .bashrc - How to find the startup file - putty

I was going over this article and it states in step 3
Add the following to your .bashrc (or the appropriate startup file for your shell) To use it immediately, be sure to type “source .bashrc”
Any idea on how I could know what my startup file is ? I am using putty ?
Once you use putty to SSH into your server, you can run "ls -al .bashrc" and it should show you the file, edit this with an editor you know, if none, then use vi like this "vi .bashrc".
Go to where you need to edit the file and type in "i" to put vi in Insert mode. Next type in your text. Once you are done press the escape button and ":wq", no quotes for the i or :wq.
Next you can source it by typing "source .bashrc" and the setting you added should be part of your BASH shell environment now.
The .bashrc is a file which is called by bash before on each start of a new interactive shell. The file can be used to setup the environment, export variables, create aliases and functions and more...
There are usually multiple instances of that file. One per system and one per user to allow system wide configuration but also customization by users ( users bashrc will be sourced after the system wide bashrc and can overwrite things). I suggest to add the lines to your user's bashrc first. The file is located in your home folder. Type:
vi $HOME/.bashrc
in order to edit the file. If you aren't familiar with the vi editor you can choose an editor of your choice like nano, mcedit or even a GUI text editor, but mind that a GUI editor's file dialog may hide the file because it's name starts with a .
Once you managed to edit the file, start a new connection or simply type
source $HOME/.bashrc
in order to parse the file
A path which will work with any bash shell regardless of operating system (macOS/Linux/BSD etc.) is:
~/.bashrc
check your home directory ...because it exists in user's home directory.
check /home/username/ on your terminal if you are using RHEL or CentOS.
.bashrc and .bash_profile are bash config files (bash shell script) that bash runs(execute) whenever it is started interactively. It initializes an interactive (non-login) shell session and the config is read from these files $HOME/.bashrc
.bashrc is a standard hidden file located in your home directory.It determines the behaviour of interactive shells.
.bashrc runs on every interactive shell launch.If you say: $bash
For login shells, the config is read from these files:
/etc/profile (always sourced)
$HOME/.bash_profile (the rest of these files are checked in order until one is found,then no other are read)
$HOME/.bash_login
$HOME/.profile
For example: I added an echo to my .bashrc and .bash_profile files and whenever I called bash or bash -l command in terminal it showed me the echo.

Resources