As I understand, if you create an alias for a directory in your .zshrc file, that directory is now considered a "named directory". I could be mistaken there, so let me know.
Based on the documentation here, it sounds like if you use "%~" in your zsh prompt and the current working directory is a named directory, then the prompt should show the alias for the directory, rather than the full path (i.e. ~{alias}). This does not seem to be working for me currently and I'm having a hard time finding examples or posts regarding this.
In my .zshrc file I have an alias like repo = "~/src/apps/repo". My hope is that when I am in that directory, the zsh prompt would show ~repo, but it always shows the full thing.
Any help is greatly appreciated!
In zsh, you can add a named directory with hash -d repo=~/src/apps/repo (not an alias). Also, ~ isn't expanded inside quotes, so you should remove them or use $HOME instead.
hash usage: https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html#index-hash-1
Static named directories: https://zsh.sourceforge.io/Doc/Release/Expansion.html#Static-named-directories
Related
(First post, be gentle.)
In a bash script, I am using:
#!/bin/bash
baseDir=$(dirname "$BASH_SOURCE")
I have saved the script as /path/to/location/script.command, so that a double-click in Finder in macOS will execute the script.
I have created symlinks to that script from multiple other locations, but no matter the location, $baseDir always returns as /path/to/location.
However if I save the script as /path/to/location/script.sh and create symlinks to that in, for example, /path/for/symlink, then $baseDir returns as my desired outcome, /path/for/symlink.
My assumption is that symlinking a .sh will always run said script from the location of the symlink, whereas symlinking a .command will always run said script from the location of the linked script.
Unfortunately, this solution won't work because I need to resolve symlinks, and this solution won't work because $0 returns the same as $BASH_SOURCE.
By recommendation of Charles below, I've scoured this FAQ, but unless I'm missing something, all the suggestions would require one of $0, $BASH_SOURCE, or PWD to return the path to the symlink, not the path to the linked script.
Is there any way around this?
I'm trying to configure Cyberduck to read the Bookmark files from my Dropbox folder.
This is usually accomplished by this command:
defaults write ch.sudo.cyberduck application.support.path ~/Dropbox/Cyberduck
Super easy!!
But... Dropbox has since changed and if you have a Pro Account the Dropbox folder is renamed "Dropbox (Personal)".
I've tried to do this:
defaults write ch.sudo.cyberduck application.support.path ~/Dropbox\ \(Personal\)/
And I get this error:
not parse: [...]/Dropbox (Personal)/Apps/Cyberduck/. Try single-quoting it.
I tried single quoting like this but same error:
defaults write ch.sudo.cyberduck application.support.path '~/Dropbox\ \(Personal\)/'
How can I solve this?
You can single quote escape your parentheses by wrapping them in a double quote:
Instead of: ln -s '/Users/username/Dropbox (Company Name)/' DropboxCompanyName
Do: ln -s "'/Users/username/Dropbox (Company Name)/'" DropboxCompanyName
Notice the double quotes added to /Users/username/Dropbox (Company Name)/
So I cannot find a way to do a "defaults write" to a path with parentheses, but I did this workaround and it seemed to work for me (in my case I needed to link to my enterprise Dropbox account):
cd to your home folder and create a symlink of the directory that has the parentheses:
ln -s '/Users/username/Dropbox (Company Name)/' DropboxCompanyName
At that point, I was able to do do a defaults write that wrote ~/DropboxCompanyName as part of the path and it worked just fine.
All that said, your personal folder already has a hidden symlink: "Dropbox" that's in the same directory and pointing to "Dropbox (Personal)", so you should be able to do you original command as such:
defaults write ch.sudo.cyberduck application.support.path ~/Dropbox/Cyberduck
… because the "Dropbox" part of the path should still lead to your personal folder. This both what I've observed locally (when viewing hidden files) and what Dropbox says on their site: https://www.dropbox.com/help/9031
I am assuming you are using Unix in Terminal.
I have set up a folder of the same name in my Public Folder to test as shown below.
Your current referencing to the folder would seem correct. Dragging the folder into the Terminal window current command line will automatically give you the correct referencing to that folder.
As an alternative I suggest putting the name of the folder in double quotation marks. Even though there are brackets in the name, there is no need to escape these characters in Unix in Terminal in this instance when using double quotation marks. This makes it easier to humanly type the correct reference.
To reference a folder in the image below, for example, the following referencing works for the cd (change directory command):
~/Public/"Dropbox (Personal)"
Hence I suggest try:
ch.sudo.cyberduck application.support.path ~/Public/"Dropbox (Personal)"
As for the rest of this command, I am not sure that ch is a valid Unix command. I do not yet have enough Unix experience to guide you from here.
you have to put "" in the whole address
more like this
ch.sudo.cyberduck application.support.path "~/Public/Dropbox (Personal)"
I cannot fix the command not found errors in UNIX bash.
I need to add the current directory to my search path. However, I searched in google but I cannot find anything which tells me the direct solution (since I am in the beginner level and it is hard to understand)
when I write to terminal pwd, it says
/Users/macbook
when I write echo $PATH, it says
/Users/macbook/opt/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/git/bin:/opt/X11/bin:/Users/macbook/OPT/BIN:/Users/macbook/opt/bin
How can I add the current directory to my search path?
Thank you for any help!
try this in your terminal, it should work.
PATH=$PATH:$(pwd)
If you want bash to always search the current directory first for the command, not matter in which directory you are currently in, you can add "." to your PATH variable. For that, edit the file .bash_profile or .profile file in your home directory (first one present) and add the line:
PATH=.:$PATH
Would it be possible to save certain locations that I've used recently/commonly (like /folder/folder/folder/) so that I don't have to manually navigate through each dir between my current and destination dir?
Sort of like alt-tab, but for paths. I'm on OS X, but perhaps this can be done using basic Unix skills?
Thanks!
If you set up your shell to save commands history, using Ctrl+R hotkey may save you some time, which performs search of previously executed commands as you type. Another good thing to know is that most shells provide you with file/directory name completion if you press Tab key once/twice, which is also of great help.
EDIT: you can also make some symbolic links inside single (e.g. home) directory to quickly access directories. Use ln -s <path to target file/directory> <path to link> command. Target paths could either be relative or absolute.
One way that works in most shells (but is slightly different for different shells) is the directory stack. You can use pushd to push a directory, popd to pop the top directory from the stack, and dirs to move directories around and switch to directories in the middle of the stack. I just checked, and the man page for pushd on Mac OS X is useless; use the man page for your shell (likely bash) and search for pushd, etc. there.
In Bash, you can add directories to the CDPATH variable. If you try to cd to a directory that doesn't begin with a slash then the directories listed in CDPATH are searched for a matching destination.
In zsh there are a few options.
A shell can keep a stack of the most recent few directories you've cd'ed into if you use setopt AUTO_PUSHD. With completion set up (autoload -U compinit; compinit), type cd + and press tab, then you'll get a numbered list:
~% cd /Library
/Library% cd /System/Library
/System/Library% cd +
1 -- /Library
2 -- /Users/nicholas
So the most recent directory is +1, etc. (You can reverse this, as I do, with setopt PUSHD_MINUS so you use - instead of + and the most recent directory is -1).
Another option is directory hashing; you can create pseudo-"home directories" (~whatever). Some of mine, for example:
hash -d v=/Volumes
hash -d a=/Volumes/BanjoArchive
hash -d pep=/System/Library/Frameworks/Python.framework/Versions/Current
hash -d sp=$(print /Library/Python/*/site-packages(On[1]))
So I can just type cd ~pep or cd pep (if unambiguous) or even pep (if AUTO_CD is set). In situations other than cd, you can use the directory stack with ~ as well, e.g. ~+1 or ~-1.
zsh can even replace shell variables after a ~ with the AUTO_NAME_DIRS option, though I don't use it because it would clutter the variable list. Nevertheless, here's an example:
~%setopt AUTO_NAME_DIRS
~%v=/Volumes
~%cd ~v
~v%pwd
/Volumes
zsh also supports cdpath as one of the other answers mentions. For example, I have:
cdpath=($HOME /Volumes)
so I can just use the name of a mounted volume or directory in my home directory to cd to it.
I stumbled upon Z and it has solved all of my dir-navigating issues.
Either an alias (via placing it into your ~/.bashrc or ~/.profile depending on setup):
alias godocs='cd /home/me/my_docs`
or pushd/popd as #Jeremiah suggests.
Take a look at "Cd Deluxe" for a greatly improved "change directory" command: http://www.plan10.com/cdd/.
I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.