1

I am using Cygwin and wish to run it with diff params so I can change dir as soon as I launch in Windows.

For example

bash --login -i ; cd /

or

bash --login -i ; cd /tmp

but the cd does not work, how can I pass a change directory to

bash --login -i

FYI I can't put the cd in .bash, it has to be passed in

tried this with no luck

bash --login -i -s cd /tmp;
Inian
  • 80,270
  • 14
  • 142
  • 161
born2net
  • 24,129
  • 22
  • 65
  • 104
  • May be other relevant answers: https://stackoverflow.com/questions/9637601/open-cygwin-at-a-specific-folder – Andry Dec 30 '20 at 14:14
  • While [this](https://superuser.com/questions/341534/how-to-start-an-interactive-shell-with-special-setup) solution is about zsh, the same strategy could be used for bash. See particularily the comment given by _Martin_ to the accepted answer of that question, or my answer to the question (which was not accepted). – user1934428 Sep 07 '21 at 07:54

3 Answers3

5

One hack is to use -c to change the directory, then immediately start a new shell in place of the first one. The working directory is inherited. Note that any shell (that supports exec) could be used to start the new process; once the working directory has been changed, then you can start the interactive login instance of bash.

bash -c "cd /tmp; exec bash --login -i"
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    That works for me using the 32-bit Cygwin on Windows 7 SP1 with all updates applied. @born2net, is it possible you have a `cd` command in your `.bashrc` or `.bash_profile` files? – Francis Litterio Dec 08 '16 at 21:12
  • Another approach is to use `script` utility: `script.exe -q -c "cd /tmp; CHERE_INVOKING=. /bin/bash -l"` – Andry Dec 30 '20 at 14:21
1

ok so I found a solution to what I needed. in Cygwin .bashrc I put

"$OLDPWD" and it will automatically switch me to the last directory I was in Windows before forks Cygwin.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
born2net
  • 24,129
  • 22
  • 65
  • 104
  • should be `cd "$OLDPWD"` – smaudet Sep 03 '20 at 23:27
  • 1
    As long as cygwin uses `CHERE_INVOKING` variable, then it is enough to declare it in the script: `bash -c "cd '...'; CHERE_INVOKING=. exec bash -l -i"`. The `$OLDPWD` will point a previous current directory just before the `cd "..."`. – Andry Dec 25 '20 at 18:56
1

I use .bashrc and .bash_logout to recover previous locations. In .bash_logout I store the current directory in a file:

if [ -d ~/.recent-locations ]; then
    pwd > ~/.recent-locations/locus-$RANDOM
fi

and in .bashrc I consume the stored locations:

if [ -d .recent-locations ]; then
    for x in $(ls .recent-locations); do
        dstdir=$(cat .recent-locations/$x)
        rm .recent-locations/$x
        cd $dstdir
        break
    done
fi

For this to work you need to logout explicitly using Ctrl-D or logout; on the other hand if you don't want to record your location you simply shut down the terminal window via the window manager.

etau
  • 11
  • 1