Changing login path without modifying $HOME - shell

When I start a terminal session and my shell starts I'd like it to log me in a specific directory instead of $HOME.
For example, I've noticed I often start a new shell session and move to /tmp just to clone a git repository or do some quick and temporary stuff, and I would like to be logged directly in a directory of mine like /sandbox or something at my shell startup rather than my $HOME directory.
Any of you aware of a way to achieve this without modifying my user's home directory nor adding a dumb cd /sandbox in my .zshrc ?
Thank you very much!

My preferred solution for issues like this is to use a bunch of wrapper scripts that set the desired environment properties and then exec an interactive shell, passing through any command line arguments:
#!/bin/sh
# sandbox-sh
cd /sandbox
exec bash "$#"
Even better, you can launch GNU screen or tmux instead of a single shell - any new windows you create will share the same properties as the first one. Alternatively, you may also launch a tabbed terminal emulator - any new tabs will also share the environment defined in the wrapper script.

Related

Git bash in Windows: change the default directory when opening mintty (pwd)

I am loosing a lot of time searching in internet for the following simple setting.
I installed git (and git bash) in Windows. What I want is just to open Git Bash and be in the directory I want. I don't want to change my home directory, just be in a given directory when I open the program.
More detailed, when I open Mingw / Git bash in Windows, I would like to be in the following folder:
/c/blabla/my_git_repositories
(this corresponds to the windows-style path: C:\blabla\my_git_repositories).
At the moment, I must write the following command every time I open GitBash:
cd /c/blabla/my_git_repositories
is this possible? Which file should i modify?
Actually the program is installed here:
C:\Program Files\Git\mingw64
Thanks
If you want it happening every time you start an interactive shell, add
cd /c/blabla/my_git_repositories
to your shell startup. If your install includes the manpages say man bash and find the INVOCATION section, in a decent pager just /^INV will do it. Or Google knows to interpret a search for man bash as a search for the bash manpage.
Windows doesn't ordinarily support shell logins at all so a lot of the shell's startup-circumstance detection is overkill on Windows. Executing it is extremely fast, but describing exactly how it decides takes a page or two.
The short form is, if the startup was told it's a login shell it reads the login startup files, including (your) ~/.bash_profile, which generally should check for an interactive login and if so source the normal interactive shell startup ~/.bashrc, so ~/.bash_profile should end with something like
[[ $- = *i* && -f ~/.bashrc ]] && . ~/.bashrc
and put whatever you want in ~/.bashrc. That cd, set up your prompt colors, define your favorite shell aliases, add your personal scripts directory to PATH, whatever.
Partial solution when opening the program by clicking on a link.
Right click on the link → properties → Start in: set the value to "C:\blabla\my_git_repositories".
This will work only when you open the application from that link, but could be fine (depending by your needs).

Having aliases active in a terminal session without using bashrc or bash_profile or bash_aliases?

This question is an offshoot of my question on whether there's anything wrong with having aliases on a production server.
So I tried creating a shell script with some aliases
#!/bin/sh
echo "creating aliases..."
alias f='clear;cd ..;ls;pwd'
alias ff='clear;cd ../..;ls;pwd'
Did a chmod +x al.sh, and ran the script ./al.sh, but although the "creating aliases..." statement got printed, none of the aliases worked, because they were obviously active only until the script ran.
So is there a way I can run a script containing the aliases I want, which will remain active as long as the terminal session is active? The basic idea being, not to cause problems for colleagues who use the same server.
For cases when you want to store functions and aliases just for your session, I find it quite useful to have a file with them and sourcing it when I login the server.
So just place it somewhere like:
~/nav_alias_file.sh
And then just after sshing the server type:
source ~/nav_alias_file.sh
Note by the way that, as Sundeep expressed in comments, you do not need the shebang in that file.

Unix bash alias not working after start screen

Hi I am having a problem with setting alias in mac after I start the screen command, I have alias for working with git, like
commit=git commit
they work as I expect when I start my terminal (iTerm2), but then sometimes I use screen to have simultaneous instances in remotes servers and virtual machines I work with. After this the alias disappear(command not found).
Does anyone know why or how solve it?
To make the alias work, you must use the alias command. For example, to create an alias in Bash you do:
$ alias commit="git commit"
This works temporarily ie.: in your current shell. In order to make it "stick", you must put it in your ~/.bashrc. That will make it be sourced to all instances of Bash you'll invoke during your terminal session.
When you start screen, it starts a separate Bash too, so you'll be covered.
You write something about VMs. If you need this alias to work there, you must make ~/.bashrc on these VMs to have the same aliases. But that's the other story. You should already know how to achieve what you want.
You need to make sure your aliases are defined in ~/.bashrc to ensure they get included in all logins. You can test this out: edit your ~/.bashrc to include this line:
echo "bashrc"
And then edit your ~/.bash_profile to include this line:
echo "bash_profile"
You'll see when you start screen that only "bashrc" is displayed.
See this question for much more detail on the subject.

How to run a shell script just by typing its name?

I am very stuck on a task for my controlled assessment. I have been asked to create a shell script in nano that echoes hello world and hello $user. I have set the script to be executable. (chmod 755 Script1 and chmod +x Script1). I have been tasked to be able to make the script run just by typing 'Script1' , I do not know how to set the path to do this. Any help would be greatly appreciated.
Basically you need to have it in the PATH environment variable.
Suppose it's in a folder on /a/b/folder/script.sh, try:
PATH=$PATH:/a/b/folder/
This tells the shell to look for executables there when looking for commands to run.
To make sure it's always executable you have several options:
Set the path variable in your profile using ~/.bashrc (sets it for bash, commonly done in ubuntu, which is what I use) or ~/.profile.
Set it globally using /etc/environment or others...
Move the folder to a folder already on your path (echo $PATH to see what's there already).
Note that these files are source-ed on different stages and persist on different environments.
For even more information read this.
TIP: To test that you've set the file correctly, we'll take ~/.bashrc as an example, you can use source ~/.bashrc and everything in ~/.bashrc will be loaded. That way you can be sure it won't mess things up next time you log in.
Alias the script in .bash_profile
in home path open the .bash_profile file and add the following entry,
alias Script1='/path/to/Script1'
Then reload the bash by,
. .bash_profile
After that try to call the script by using just its name. i.e, Script1

Faster use of the cli - setting a variable for current project

I'm frequently using the cli (terminal) on Linux and Mac.
How can I save time by setting a variable to the current project folder instead of
cd path/to/folder/over/and/over/again
Something like
cd the-current-project
(with tab completion)
?
Depending on your shell, you can set a cdpath with directories you regularly cd to, and so you can do just what you want:
cd the-current-project
from anywhere. See here for more details.
Alternatively you can just set a shell variable and
cd $CURRENT_PROJECT
Again, your shell will determine whether you can tab-complete this (I know zsh will do this. I'm not sure about bash).

Resources