terminal cant do ls or cd - macos

Can anybody explain what is wrong with my terminal:
$ echo $PATH
=/usr/local/bin
$ ls
-bash: ls: command not found
$ cd
-bash: find: command not found
Why won't these commands work? Help? Anyone?

I'm guessing your .bash_profile or .bashrc has a line that looks like
export PATH=/usr/local/bin
This is overwriting all the existing stuff that needs to be in your $PATH. You need to change this line to look like
export PATH=/usr/local/bin:$PATH

"ls" lives in "/bin" and "find" lives in "/usr/bin". You need to add these to your $PATH in your bash_profile or .bashrc.

Related

How to symlink alias and function dotfiles

I trying to make my own alises. I have this in my ~/dotfiles:
alias hello="echo Hello"
I try to symlink this:
ln -sn "~/dotfiles/.alias" ~
But after this when i run my alias nothing happens
Is there a path for aliases in home to symlink it?
Make sure you are sourcing your aliases file in your .bashrc or .zshrc
the code for that in your .bashrc or .zshrc should look something like this:
if [ -f ~/dotfiles/.aliases ];
then source ~/dotfiles/.aliases
fi

How do I make a command in cygwin to run Sublime Text?

I'm trying to mimic the subl command in iterm for mac computers in cygwin.
Basically, I want to be able to open a current folder from cygwin by typing subl .
I haven't found any good instructions. I know where my .bashrc file is located. I just dont know what to do to create the command subl and make it so that the path following subl opens with Sublime.
Can anyone help?
You'd want to make an alias and source it from bashrc.
Example
Create a file ~/.bash_aliases with:
alias subl='/cygdrive/c/sublime.exe' #make sure this command is correct for you
Now in ~/.bashrc do:
source ~/.bash_aliases
Log out and log back in, and subl . should work
Assuming you want correct behaviour when doing subl ~, a simple alias or adding Sublime Text to your PATH will not work.
You can use the following bash function instead (put it in your .bashrc):
function subl {
cygpath --windows $# | sed 's/\\/\\\\/g' | tr '\n' '\0' | xargs -0 -n1 /cygdrive/c/Program\ Files/Sublime\ Text\ 3/subl.exe
}
Note that when passing paths to xargs you need to 1) escape the backslashes, 2) use the NUL character as argument delimiter for it to work with paths with spaces.
Edit: Use subl.exe rather than sublime_text3.exe so that it would detach itself from the terminal.
To open files and use Git tools (commit, rebase, tag, ... messages):
#!/bin/bash
# To create in [.babun/]cygwin/usr/local/bin/subl with chmod +x
ARGS=""
while test $# -gt 0
do
ARGS="$ARGS ${1#/cygdrive/[a-zA-Z]}"; # Remove /cygdrive and disk letter from the path
shift
done
/cygdrive/c/Program\ Files/Sublime\ Text\ 3/subl.exe $ARGS
https://gist.github.com/cmalard/16c58869319c9a88473ec08cc7989c6b
You can also simply add Sublime to your PATH variable. For cygwin, you can:
Go to your home directory and verify .bash_profile exists with ls -a
Open .bash_profile with something like vim .bash_profile
add the line export PATH=$PATH:"/cygdrive/c/Program Files/Sublime Text 3" (or whatever top level installation folder Sublime is in)
Reopen your terminal and use subl to verify it works

cat a file to a variable

Assuming the name of my script is myscript.sh and my current directory is /Users/scripts/ I'm trying to do the following:
localScript=$(cat ./myscript.sh)
I get the following error:
#!/bin/sh not found
I can't seem to figure out how to do this, but I assume its not working because $() is creating a subshell that has a different pwd and thus cannot find my file.
I've also tried using various combinations of pwd but I'm having trouble with this method as well.
On OSX I've done the following:
$ vim test.sh
and typed in the following:
#!/bin/sh
localScript=$(cat ./test.sh)
echo $localScript
and then,
$ chmod +x test.sh
$ ./test.sh
which gives the following output:
#!/bin/sh localScript=$(cat ./test.sh) echo $localScript
Maybe the above will help you spot your error.

Mac terminal keeps giving message

Whenever I open my terminal on my mac I get this message, -bash: /usr/local/bin: is a directory
How do I remove this? As I find it annoying and unneeded.
There is probably a line trying to execute /usr/local/bin (which is a directory and not a executable) in either your ~/.bash_profile or your ~/.bashrc
If you want to view the contents of these files (as you asked above) you can type in your terminal after you open it:
cat ~/.bash_profile
and
cat ~/.bashrc
You can try to verify what lines mention /usr/local/bin by typing these commands:
cat ~/.bash_profile | grep "/usr/local/bin"
and
cat ~/.bashrc | grep "/usr/local/bin"

bash - cd command not working?

I've somehow managed to screw up bash while fiddling with the $PATH variable in my bash_profile (I think...). All I did, as far as I can remember, was add a directory to the $PATH variable. Please HELP!
Here's what I get when I cd into various directories
my-MacBook-Pro:~ myuser$ cd .rvm
-bash: dirname: command not found
-bash: find: command not found
my-MacBook-Pro:.rvm myuser$ cd
-bash: find: command not found
And here's what happens when I try to get into my .bash_profile to undo whatever it is that I did...
my-MacBook-Pro:~ myuser$ emacs .bash_profile
-bash: emacs: command not found
my-MacBook-Pro:~ myuser$ sudo emacs .bash_profile
-bash: sudo: command not found
Any help would be massively appreciated. I'm completely screwed until I can get bash working normally again!
/usr/bin/emacs .bash_profile or similar should work when the PATH is broken.
The $PATH variable tells the shell where to look for commands. If you just bypass that by telling it the full path, it should work. Try /usr/bin/emacs .bash_profile.
When you do a cd, you're getting a bunch of other things. Since you're using BASH there are are two possible issues:
You have PROMPT_COMMAND defined. Try to undefining it:
$ unset PROMPT_COMMAND
There's an alias of the cd command: This was quite common in Kornshell where you don't have the nice backslashed characters you could put into your prompt string. If you wanted your prompt to have the name of your directory in it.
You had to do something like this:
function _cd
{
logname="$(logname)"
hostname="$(hostname)"
directory="$1"
pattern="$2"
if [ "$pattern" ] #This is a substitution!
then
\cd "$directory" "$pattern"
elif [ "$directory" ]
then
\cd "$directory"
else
\cd
fi
directory=$PWD
shortName=${directory#$HOME}
if [ "$shortName" = "" ]
then
prompt="~$logname"
elif [ "$shortName" = "$directory" ]
then
prompt="$directory"
else
prompt="~$shortName"
fi
title="$logname#$hostname:$prompt"
PS1="$title
$ "
}
alias cd="_cd"
Ugly isn't it? You don't have to go through all of that for BASH, but this does work in BASH too, and I've seen places where this was done either out of ignorance of inertia.
Try this:
$ type cd
You'll either get
$type cd
cd is a shell builtin
or you'll get
$ type cd
cd is an alias for ....
As for your updating of $PATH, you probably forgot to put $PATH back in the new definition, or quotation marks because someone has a directory name with a space in it. Your PATH setting should look like this:
PATH="/my/directory:$PATH"
Some people say it should be:
PATH="$PATH:/my/directory"
I guess, that you have defined $PROMPT_COMMAND (maybe in .bashrc) in a way that uses dirname and find.
That would explain the behavior of cd.
The find command is by default in /usr/bin/find. Thus, you can use it to find the locations of your imprtant commands and reconstruct you path information.

Resources