Assign directory to variable in a source file - linux

I am building a source file with some alias to executable files (these are working just fine) and assigning directories to variables in order to get to the directory quicker, with less typing. For example, if I source example.source:
#!/usr/bin/bash
mydir="/path/to/some/dir"
I can get to /path/to/some/dir with
cd $mydir
However, I am not being able to use tab complete to navigate through other sub-directories like I would do by typing the complete path. I mean, if I use the tab key to complete the variable I get cd $mydir but not cd $mydir/ (I have to delete the last space character and manually type the slash / to see the next sub-directories). Hope this is an understandable question. Is there any workaround for this?
EDIT: the linux distribution I'm using is Slackware Linux 3.2.31.c x86_64 GenuineIntel GNU/Linux
EDIT2: GNU bash, version 4.2.37(2)-release

Apparently this feature is starting to be implemented in bash 4.3, release 26-Feb-2014 09:25.
Reading the NEWS file in bash 4.3 I found this:
i. The word completion code checks whether or not a filename
containing a
shell variable expands to a directory name and appends `/' to the word
as appropriate. The same code expands shell variables in command names
when performing command completion.
Unfortunately I cannot do a de novo installation of bash (because I'm working on a server) but I hope this can help others.

If I understand your question, then I believe it can be solved by putting this at the top of your example.source. This will list your contents every-time that you cd.
#!/usr/bin/bash
# Make cd change directories and then list the contents
function cd() {
builtin cd $*;
ls;
}
mydir="/path/to/some/dir"
cd $mydir
My other suggestion is to try to put cd within your alias. Something like this:
mydir="cd /path/to/some/dir"
$mydir

Related

on the "local" toolchain in linux

long time lurker first time poster.
I've looked everywhere and I'm sure it's easily found but I couldn't properly word the search.
I working on some coding exercises from a textbook and I've got all my work in directories with the following hierarchy:
r00t
/ \
tools code
\
chapts
In the tools directory are a couple scripts to make my life easier. What I want, and the reason I'm posting, is to be able to call the scripts in r00t/tools wherever I am, as long as I'm inside r00t. I know I could just add them to the global PATH but that seems lazy and I don't want my PATH to balloon any more (is this even sensible?).
So, can I add scripts or programs to the "local PATH" inside the dir somehow?
Take a look over here towards the ondir (link) program letting you execute scripts upon entering or leaving directories.
With that you could dynamically change your path variable. I must however admit to never have used the program nor to be informed about its development status.
Alternatively you could replace your cd command with a script checking pwd versus your r00t directory and updating $PATH based on the outcome. of course your cd will be slowed down but probably not even noticeably.
an example:
#!/bin/bash
#alternative cd
cd $*
#check for r00t directory
if [ "$( pwd | grep -o 'r00t')" == "r00t" ] ; then
#check path variable:
if [ "$(echo $PATH | grep -o 'r00t')" != "r00t" ] ; then
export PATH=$PATH:/DIR/r00t/bin
fi
else
#remove r00t from PATH when not in r00t
if [ "$(echo $PATH | grep -o 'r00t')" == "r00t" ] ; then
export PATH=$( echo $PATH | sed 's~:/DIR/r00t/bin~~' )
fi
fi
note that now you'll have to intoduce the alias as follows:
alias cd='. ./path/to/script/alternative_cd.sh'
as the exported PATH needs to be sourced in order to work for your current shell (if using bash alternative_cd.sh, you'd only get the new PATH for the subshell the script is run in)
I tested it and it seemed to be working. Have fun.
The usual way to handle this is to put an rc file in the root directory of each of your project directory trees. In this case, r00t. Call the file r00trc or make your own naming convention.
The rc file should include an identifying change to the command line prompt in order to remind you constantly what environment the shell is using, and a re-assignment of the PATH variable to suite the needs of the project, and any other project-specific environment variables, aliases or color setting that you need.
From your default login environment, spawn a sub-shell by running 'bash' either before or after or without changing the current working directory to r00t and then source the r00trc file. This provides you with a Bash shell with the project environment and an identifying prompt. Use exit to exit the sub-shell and return to your default environment.
Avoid the temptation to collect your project rc files in your home directory or anywhere other than the root of the project directories so that they do not get lost when you tar up a project and archive it or send it to a colleague.

Im using touch to create a file in a folder on my desktop... Right when I tried this it says command not found

At first I had
touch $NAME_OF_FILE$DATE.$FILE_EXT
then I changed it to
PATH="Logs/"
touch $PATH$NAME_OF_FILE$DATE.$FILE_EXT
The file is created correctly in the folder, however only echos are being printed in there because says commands are not found like grep, awk, and others.
EDIT: The folder is already created on my desktop
Thanks
Alan
PATH is an environment variable that specifies where executables are located and is used by your shell to look for commands executables (grep, awk, ...). You should not override it in your script.
Try:
MYPATH="Logs/"
touch $MYPATH$NAME_OF_FILE$DATE.$FILE_EXT
To understand what PATH is open a shell and type echo $PATH. You will see it contains the directories where your commands executables are.

Add a bash script to path

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $# )
len=${#array[#]}
_args=${array[#]:1:$len}
sudo http_proxy="http://user:password#server:port" apt-get $_args
Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
Try this:
Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
Then you can just run apt-proxy with your arguments and it will run anywhere.
Note that if you export the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary e.g.
PATH=$MYDIR:$PATH
where MYDIR is defined as the directory containing your binary e.g.
PATH=/Users/username/bin:$PATH
You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.
Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.
As a final step, after following the solution form proposed by #jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:
chmod -R 755 ~/bin
make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.
adding to #jlhonora
your changes in ~./bashrc or ~./zshrc won't reflect until you do
source ~./zshrc or source ./bashrc , or restart your pc

How do I change the directory in Git Bash with Git for Windows?

How would I change to the directory C:/Users/myname/project name in Git Bash?
cd /c/users/myname/project\ name
Beware that ls /, or typing cd / followed by Tab-completion, might not show the existence of this folder, but cd /c will still work. Also note that pwd (to print the current working directory) might show something like /bin, but this might not be the actual working folder for commands such as git clone, which might use the folder from which Git Bash was started.
If the you know how many levels up from your current working directory, you could use cd ../project/name to avoid writing the entire directory path.
The .. represents moving 1 directory up.
You will need to use quotes in your directory name, or the short version of the filename.
You can find the short version of the file name by issuing the command:
dir /x
If I remember correctly. I do not have a windows machine.
It is a version of bash shell though, so you should be able to simply quote it. (And the dir /x may or may not work.)
If you are at the a directory and wanna switch to sub directory use :
cd "project name"
If you wanna go to a different path use the whole path :
cd "C:/Users/myname/project name"
But you can avoid use white spaces in project files and folders and instead use underscore
An alternative that worked for me (Windows 10 x64) is putting the full address in quotes:
cd "D:\BWayne\Documents\- School\Developer\-- Backend\Test for GitBash"
I could then do like mkdir, touch, etc and it successfully put them in the Test for GitBash folder.

alias - cd followed by ls

How can I define an alias so that when I do cd Abcd, where 'Abcd' is the name of a directory, the directory is changed to 'Abcd' and is followed by ls to show the contents of the directory?
I believe you can't use an alias to accomplish that, but you could define a function to do it:
#print contents after moving to given directory
cl()
{
cd $#
ls
}
You could stick this in your ~/.bashrc file.
If you were hoping to override the builtin cd command, then you could do:
#print contents after moving to given directory
cd()
{
builtin cd $#
ls
}
UNIX
Creating an alias
Your Linux distribution will most likely not have the .bash_aliases file created in your home, or you can even create it manually. To create the file, type in the following command:
touch ~/.bash_alisaes
Now that file will be executed automatically every time you fire off a new Terminal.
What you can do now is create a list of aliases and add them to that file for later uses.
create an alias and update the ~/.bash_aliases file to make it permanent.
Generic approach: Creating a custom script
Create a bash script in your /usr/bin folder, it should look something like this
#!/bin/bash
Whatever combination of commands you want to run when you type this thing.
Its really that easy.
Just name the bash script what you want to type in to the terminal, and make it excecutable: chmod +x filename and you're good to go!
WINDOWS
You can use DOSKEY command:
From Wikipedia:
DOSKey is a utility for MS-DOS and Microsoft Windows that adds command
history, macro functionality, and improved editing features to the
command line interpretersCOMMAND.COM and cmd.exe. It was included as a
TSR program with MS-DOS and PC-DOS versions 5 and later, and with
Microsoft's Windows 95/98/Me.
For example: To create a macro that quickly and unconditionally formats a disk, type:
doskey qf=format $1 /q /u
To quickly and unconditionally format a disk in drive Z, type:
qf Z:
To define a macro with multiple commands, use $t to separate commands, so the solution to your problem follows:
doskey cd=cd $1$tdir
Now, this will work only in your currently open command window. To make it permanent simply create a batch file and set the value of the absolute path of the file to the regedit
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
Source for the regedit: superuser.com/a/238858

Resources