For some reason tilde expansion in Cygwin is not working for me at the prompt:
$ ls ~
ls: cannot access : No such file or directory
$ ls ~/bin
(lists /bin not $HOME/bin)
$ echo $HOME
/home/myusername
$ echo ~
$
In the last case there (echo ~) there is no output (other than a couple of blank lines.)
I have set expand-tilde on set in my $HOME/.inputrc file. Is there something else I need configured?
This is a recurring problem with Cygwin that, I believe, was tied to some upgrade bug a while ago and never resolved.
In any case, the solution is simple: open /etc/passwd in your favourite editor, and on the line with your username, between the fifth and sixth colons (the last and second-to-last), write in the correct path to your home directory, ie /home/myusername.
The relevant line in my /etc/passwd looks something like the below:
meand:unused:12345:54321:PCNAME\meand,S-1-5-21-4567891230-654987321-312456978-58252:/home/meand:/bin/screen
Related
This is a follow up to https://apple.stackexchange.com/questions/52459/ and is about an unexpected behavior in bash. To summarize what's in that link, the problem is to copy the current directory in Terminal to a temporary variable, say the pasteboard, and use that to switch directory in a different Terminal window. The solution provided there pretty much nails it in the most efficient way! However, when I actually try changing directories using this temporary variable with the correctly escaped directory name, it seems to not work right in bash.
My minimum working example is as follows:
alias cwd='printf "%q/\n" "$(pwd)"'
Now in a terminal:
>$ mkdir tmp
>$ cd tmp
>$ mkdir test\ dir
>$ cd test\ dir
>$ cwd | pbcopy
In a new terminal:
>$ echo "$(pbpaste)"
/Users/foo/tmp/test\ dir/
>$ cd $(pbpaste)
-bash: cd: /Users/kaushik/tmp/test\: No such file or directory
>$ cd "$(pbpaste)"
-bash: cd: /Users/kaushik/tmp/test\ dir/: No such file or directory
I'm quite at loss in trying to figure out what I'm doing wrong. The only thing I'm certain of is that this is a bash problem and not something that's cropping up on OS X.
Thanks for your help on this and, by the way, it turns out that I had to finally, after all these many years, end up writing my first stack overflow post!
Copied from comments: The linked answer specifically asks for the escaped PWD suitable for pasting, but you want a programmatic input where escaping is counter-productive. Just do pwd | pbcopy and cd "$(pbpaste)".
EDIT:
(To be honest, I presumed you would need to escape it explicitly since that's how I create a directory with spaces using pwd.)
The issue is that command-line parser only does one pass of unescaping. In case of cd foo\ bar, the space is unescaped. In case of cd $(pbpaste), there is nothing to unescape; then pbpaste's literal output is put into the argument list.
I am on a RedHat csh.
I just modified ~/.cshrc with a wrong path and resulting in an syntax-error.
my shell just not recognize ls or gedit. So, when I again try to modify, it is not again opening with gedit ~/.cshrc.
When I boot the system it is not starting the profile.
I Have the root access and root profile is fine. Can I edit it from root.
Please help me to remove last two lines i have added to /.cshrc, using root-profile so that it works normal as previous.
thanks.
I'm guessing your PATH isn't getting set properly due to the syntax-error in your .cshrc (assumption since you mention ls isn't working). Try the following from your shell:
echo $PATH
echo is a built-in and should always work. If it returns nothing, is empty, or doesn't include a list of paths similar to /bin, /usr/bin, etc ... then your path is indeed incorrect. Use the full path to gedit on the command line like:
/usr/bin/gedit ~/.cshrc
Is there a way to get my ubuntu command prompt to be something like:
user#host:home$
but still have the behavior of:
user#host:home$ cd projects
user#host:home/projects$
Right now, my working directory path before the home directory is cluttered with stuff from my school's servers (e.g. user#host:/blah/blah/blah/home$ ). Is there a way I can still append the directory path to the prompt as I navigate around, but just trim the beginning stuff off?
You can create a function and place it in your rc file (perhaps ~/.bashrc) like this:
function simplify_dir {
local PREFIX
if [[ $PWD == "$HOME"* ]]; then
PREFIX=${HOME%/}
PREFIX=${PREFIX%/*}
echo "${PWD##"$PREFIX/"}"
else
echo "$PWD"
fi
}
Also add this command to enable expansion in prompt:
shopt -s promptvars
Then set your PS1 prompt to something like this:
PS1='\u#\h:$(simplify_dir)\$ '
Try out this one:
PS1="[\u#\h:\w ] $ "
Case matters:
\w : the current working directory, with $HOME abbreviated with a tilde
\W : the basename of the current working directory, with $HOME abbreviated with a tilde
I have several similarly structured directory trees.
something like:
~/
Tree1/
src/
bin/
Tree2/
src/
bin/
When I somewhere below Tree1/src I want to work with Tree1/bin. When I somewhere below Tree2/src I want to work with Tree2/bin.
Is there a way to define a shell variable whose value depends on my current working directory?
PWD is a variable already set to your current directory by bash, ksh and other shells.
cwd=$(pwd) should do the trick. It assigns the output of print working directory (pwd) to a variable.
To replace ~Tree1/src/dir1/dir2 with ~Tree1/bin you could do
bindir=$(pwd | sed 's/src.*/bin/')
See also Command Substitution
As jlliagre stated, bash (as many other modern shells) stores the current working directory in $PWD; if it is Tree1/src/some/other/directory, then you can extract "Tree1/bin" from it by just using "parameter expansion":
$ echo $PWD
Tree1/src/some/other/directory
$ echo ${PWD%%src*}bin
Tree1/bin
Generally $PWD variable (Present Working Directory) contains the path to the current directory. If this variable is not defined, you can use the pwd command that will return the current path.
Two other definitions of "current" include the directory you were in when the script was started (which is the value of start_dir="$PWD" at the start of the file, no matter where the script is) and the directory of the script itself - script_dir="$(dirname -- "$(readlink -f -- "$0")")".
In my bash script I need to change current dir to user's home directory.
if I want to change to user's foo home dir, from the command line I can do:
cd ~foo
Which works fine, however when I do the same from the script it tells me:
./bar.sh: line 4: cd: ~foo: No such file or directory
Seams like it would be such a trivial thing, but it's not working. What's the problem here? Do I need to escape the "~" or perhaps missing quotes or something else?
Edit
when I say user I don't mean current user that runs the script, but in general any other user on the system
Edit
Here is the script:
#!/bin/bash
user="foo"
cd ~$user
if username is hardcoded like
cd ~foo
it works, but if it is in the user variable then it doesn't. What am I missing here?
What about
cd $(getent passwd foo | cut -d: -f6)
and
USER=foo
eval cd ~$USER
works, too (foo is the username)
Change it to:
cd $HOME
Actually, I'm not sure why cd ~whatever wouldn't work. I've just tested with a small script and it worked fine:
#!/bin/bash
cd ~sbright
I actually get the same error message that you do when the specified user does not exist on the system. Are you sure (and yes, I know this is one of those is-it-plugged-in questions) that the user exists and has a valid home directory specified?
Edit:
Now that I see what you are actually doing... tilde expansion happens before variable interpolation, which is why you are getting this error.
Is the script going to be run by the user? If it is you can just do:
cd ~
Is there some reason you can't do:
#!/bin/bash
cd /home/$USER
Of course directories aren't in /home on all *nixes, but assuming you know what OS/distro your script is targeted for, you should be able to come up with something that works well enough.