How to set custom variables for directories in shell? - shell

so I'm new to the world of Linux. Due to my new internship however, I have to work a lot with it and thus also get a hang of using the terminal appropriately.
Is there a way to set path-variables for faster navigation through directories? Similar to using '~' as an abbreviation for '/home/usr/'.
So far, I have tried to use:
```
name#torch:~$ export var=/home/usr/where/i/want/to/go
name#torch:~$ cd $var
name#torch:~/where/i/want/to/go$ ...
```
This option works only temporarily in a single shell but is not adapted when closing down the terminal or starting another one next to it. Is there a way to define more general path-variables?
Best wishes,
Hauke

Related

Bash import or export variables

I am adding commands into my bash profile and am trying to make my code simpler and want to delete the extraneous duplicated code. I have export statements and I have the commands running correctly but I am trying to simplify my code. I am using the same exact code for my commands and want to make it into one file. I am running the commands on a Mac terminal. I have installed npm, atom, atom shell commands and am on Mac OS version 10.14.5.
I tried changing the code structure around but I cannot figure out how to simplify and reuse the commands and make them into a function. The commands are duplicates of each other except that they are pointing to different file paths. I do not want to keep on reusing the same code and I am hoping to make it simpler but not sure how.
export snippet_project="/Users/kevinpleong/Desktop/my-programming-projects/snippet-creator-electronjs"
export programmingprojects="/Users/kevinpleong/Desktop/my-programming-projects"
export personal_website="/Users/kevinpleong/Desktop/my-programming-projects/personal-webiste"
edit-snippet-creator() {
cd /Users/kevinpleong/Desktop/my-programming-projects/snippet-creator-electronjs
atom .
}
edit-personal-website(){
cd /Users/kevinpleong/Desktop/my-programming-projects/personal-website/
atom .
}
edit-programming-projects(){
cd /Users/kevinpleong/Desktop/my-programming-projects/
atom .
}
I am hoping that it outputs the same thing as the bottom 3 blocks of code but I can simplify the code.
The code can be simplified to this:
projects_directory="${HOME}/Desktop/my-programming-projects"
edit-snippet-creator() {
atom "${projects_directory}/snippet-creator-electronjs"
}
edit-personal-website(){
atom "${projects_directory}/personal-website"
}
edit-programming-projects(){
atom "$projects_directory"
}
The exports are redundant
The variables are redundant since they would only each be used once. Only the projects directory is reused.
Trailing slashes are unnecessary.
Use More Quotes™.
That said, as you get used to the shell I expect you'll want to move away from patterns like this, because they are a premature abstraction and they hinder you from learning all the powerful built-in features of the shell, such as Tab completion. For example, if you've already edited a project you can press Ctrl-r to search the command history for the name of that project. Also, I expect atom, like every sane shell command, can take a directory different from the current one as the target, so you can simply type atom ~/Desktop/my, press Tab to complete atom ~/Desktop/my-programming-projects/, and press Tab again to see which projects are available.

Add Path to OSX to El Capitan

I am trying to learn UNIX.
I am using a book called “Wicked Cool Shell Scripts”.
I am told that .bash_profile contains my login for bash and that I can add paths to it so that commands I enter in Terminal will find the scripts I am writing.
The contents of my current bash_profile is:
export PATH=~/bin:$PATH
When I type echo $PATH I get:
/usr/local/opt/php#7.0/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.3/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
I would like to add a path so that - as the book suggests - I can write scripts and refer to them directly from the command line, instead of having to constantly navigate to that directory to run them.
I have a file with a shebang. It runs fine when I type its name and am in the same directory. I have moved that file to the folder scripts, which is located under crg/Users/ (ie: Users/crg/scripts)
According to this book, I can now alter my $PATH to include that directory, so that when I type that filename, the program will run.
I cannot do this successfully.
I don’t know why.
After every edit, I quit terminal and reopen it, to ensure it is using the newly edited bash_profile.
As per the books instructions on page 5, I have tried entering this in my bash_profile:
export PATH=”/Users/crg/scripts/:$PATH”
I save my bash_profile, quit Terminal, reopen it and type echo $Path
This is the result:
”/Users/crg/scripts/:??
This is not right. In fact, it's wildly wrong. And it does not allow me to run scripts from the folder indicated. It also seems to completely overwrite whatever was in the bash_profile before this, so I cannot - after doing this 'simple edit' suggested by a 'professional' - run a php -version command from the Terminal.
I am at a complete loss as to why this is happening.
Why is there a quotation mark at the beginning of this line (but not at the end)?
What's with the colon and the 2 question marks at the end of this line?
How do I add/append a path to my bash_profile?
More questions:
When I try and solve this on my own using “the Internet”, I discover many interesting versions of this ‘simple’ process: Here’s one suggested by a ‘professional’:
export PATH="${PATH}:/path/to/program/inside/package"
This is very different from what the book says...
Here’s another version of ‘how to do it’ by ‘a professional’:
export PATH=$PATH:/usr/local/sbin/modemZapp
Notice that this one doesn’t even have quotes. In both, the PATH variable comes before the actual path.
Why are there so many 'versions' of how to perform this simple task?
Can someone please tell me how to add a path to my .bash_profile?
UPDATE: I have followed the advice here, (add it to etc/paths) but this does not work either.
I get the exact same thing when I type echo $PATH in a new Terminal:
/usr/local/opt/php#7.0/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/Library/Frameworks/Python.framework/Versions/3.3/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr:/usr/local/share/npm/bin:/Users/fhb/scripts:/opt/X11/bin
I can't help but note a comment on that last page: "I have been through at least a dozen different methods for adding directories to $PATH. Why are there so many and why do so few of them work?"
To answer your first question if you look closely your quotes are ” instead of ". I'm guessing you edited either your bash_profile or this post in a rich text editor instead of a plain text one. I would recommend notepad for Windows or nano for *nix if you are writing code. To fix this issue, replace the ” with ".
To answer your second question, bash is quite forgiving and will allow you to set a string variable in multiple different ways, even without quotes. However you can run into issues when a string contains whitespace, for example: /Users/lilHenry/my scripts.
The "${PATH}" syntax is just another way to declare a string. It has the benefit that it allows you to interpolate variables into a string like so:
prefix="foo"
echo "${prefix}bar"
This will output foobar, whereas echo "$prefixbar" will not output anything as the variable prefixbar has not been set. I would suggest sticking with the export PATH="/Users/me/bin:$PATH" syntax.

Find out where an environment variable was last set in bash

Okay I know there is a bash debugger. But what I'm seeking is if I had an environment variable in one of my startup scripts and I don't know how it was set or where it might be, is there a way to find it other than exhaustively searching the scripts?
I mean is there a mechanism/tool that provides such a thing? Does bash keep track of variable setting locations?
Even though this might not seem very important but it crossed my mind the other day when I was helping a friend install OpenCL and the package supposedly set the variable $ATISTREAMSDKROOT automatically. Anyway the package was supposed to add a file to /etc/profile.d to allow for setting the variable, but it didn't. And luckily the variable came out blank.
But I was wondering if it hadn't come out blank, and the package added it to some random file, I would have probably had no way of telling where it is other than looking for it.
Of course I know one could write a sed command or two and search through the scripts but I'd consider that exhaustive search :D
One option would be to start an instance of bash with:
bash -x
... and look for where the variable is set in that output. To redirect that output to a file, you could do:
bash -x -ls -c "exit" 2> shell-startup-output
You should see in the output where each file is sourced.

Can I use what I wrote on the shell (bash, cmd, irb, etc) in a script automatically?

The general idea is pretty simple, I want to make a script for a certain task, I do it in the shell (any shell), and then I want to copy the commands I have used.
If I copy all the stuff in the window, then I have a lot of stuff to delete and to correct. (and is not easy to copy from shell)
Resume: I want to take all the things I wrote...
Is there an easy way to do this easy task?
Update: Partial solution
In bash, the solution is pretty simple, there is a history command, and there are ports of the idea:
IRB: Tweaking IRB
Cmd: Use PowerShell -> Get-History (or use cygwin)
Another Update:
I found that doskey have a parameter history to do this:
cmd: Doskey /history >> history.cmd
Yes, you can use:
history -w filename.sh
This will save your command history to filename.sh. You may need to edit that to keep just the lines at the end that are part of your command sequence.
NOTE: This is a bash command and will not work with all shells.
script may help here. Typing script will throw you into a new shell and save
all input and output to a file called typescript. When you're done with your interaction,
exit the shell. The file typescript is then amenable to grep'ing. For example, you might
grep for your prompt and save the output to the file. If you're a clumsy typist like me, then you may need to do some cleanup work to remove backspaces. There used to be a program that did thisbut I don't seem to find it right now. Here is one I found on the
'net: http://www.cat.pdx.edu/tutors/files/fixts.cpp
This approach is especially useful if you want to track and post on the web an entire interactive session.

Coloring directory name in ksh

In my current situation, it is not unusual for me to have several UNIX computers I connect to, as several different users depending on the situation, and to traverse through various directories on the machines doing things. I use ksh through the entire thing.
I was fiddling with my prompt recently, and I was able to get it to change some colors depending on my current username and current server. However, what I would also want is for it to change colors based on my current directory. For example, if I were in directory "foo", the prompt should be yellow, but if I were in directory "bar", the prompt would be magenta. In both cases, subdirectories should also count, so a simple substring check should be enough.
The problem I ran into, however, is that when I run my .profile script, it properly colors the directory--but it no longer dynamically updates whenever I switch to another directory--and I'm not sure how before I did all the branching, I was able to get it to print my current working directory correctly even after I switched directories.
I did some googling, and find information for bash, but ksh seems to be largely ignored. As I cannot figure out how to do this on my own, I must bring it to the Stack Overflow community, to add it to future knowledge. Thus, with my long-winded explanation, the "quick version" of my question is as follows:
In ksh, how can I set up my prompt to display the current working directory and color the text based on where the current working directory is? Is it even possible?
Why not using zsh? It is based on ksh, and it is much more powerful. In zsh you can write chpwd function that is implicitly called every time you change directory. In this function you can check your current directory and set PS1 to whatever you want.
Alternatively (even in ksh) you can create an alias for cd command:
change_my_ps() {
PS1=...
}
better_cd() {
builtin cd "$#"
change_my_ps
}
alias cd=better_cd
Something like this. I'm not sure it is proper, I don't remember ksh syntax.
I was able to dig up a semi-solution here:
http://books.google.com/books?id=QYu_v2R6fIQC&pg=PA71&lpg=PA71&dq=korn+dynamic+prompt&source=bl&ots=yMEZiWrGyU&sig=8KBbs12Mtk3eGNSZQiLVmFYZVFY&hl=en&ei=2HX4Sej0K6LWMP2NxakP&sa=X&oi=book_result&ct=result&resnum=2#PPA72,M1
Though enacting it is still difficult.
To display the current directory in ksh, put this in your .profile file:
export PS1="\$PWD "
That will dynamically update when you change directory without mucking around with functions.
Since nobody actually answered the part about the color, here's how I'd do it. I can't color the text, so I've also included a screenshot of my terminal.
See Wikipedia's page on ANSI escape codes for the full list of color codes and print --man and printf --man for the details of printing escape sequences in ksh.
(Note that ksh93v, currently in beta, will contain a prompt language based (I think) on bash's, which will make this sort of thing somewhat easier, though I think you'll probably still need to use something like this function to do complex conditional prompts like this one.)
> cat ~/scripts/prompt
function prompt
{
set -eu
typeset c=
case $PWD in
*/foo) c=3;; # yellow
*/bar) c=5;; # magenta
esac
print "\E[3${c}m$PWD\E[m > "
}
PS1='`prompt`'
> . ~/scripts/prompt
/Users/adavies > cd foo
/Users/adavies/foo > cd ../bar
/Users/adavies/bar >
I use this:
function chdir
{
cd "$#"
CWDH=${PWD%/*}
PS1="($_time)$hname:${CWDH##*/}/${PWD##*/} ->"
export PS1
}
alias cd=chdir
chdir .
Ignore the time and hname, but the rest should work for you. Changing colors is going to be terminal dependent. You need to know the escape codes for each color for the terminal you will be using. If you know you only ever use an xterm, it will be easier.

Resources