I am currently working on an embedded Linux system. For it to be similar to our other products I need to have it start a shell in a specified directory on boot, accessible using the serial port.

For that I have this line in the inittab-script:


This is working so far, only that the shell starts up with a pwd of /, instead of /mnt/flash.

The only way I can come up with is to have it not start /bin/sh but instead a script like that:

cd /mnt/flash

Is there a way to do that in-line in the inittab without a second script?

Edit: I need this to be a login script. This is what the - before the /bin/sh signifies. If I just run ::respawn:/bin/sh -c "cd /mnt/flash;exec /bin/sh" it does change the folder as expected, but I don't get a login shell which causes other problems.

If I run it with ::respawn:-/bin/sh -c "cd /mnt/flash;exec /bin/sh I get this error:

/bin/sh: exec: line 1: -/bin/sh: not found
  • 2
    End your script with exec /bin/sh instead of just /bin/sh Jun 30 '15 at 10:44

You could give the shell some arguments, so that it starts slightly differently. eg.

/bin/sh -c "cd /mnt/flash;exec /bin/sh"

Starting with -c which will execute commands in following string.

First command is the directory change, followed by exec which will start a new shell (in the same process) which is now starting in your desired directory.


If busybox shell is being used, there is a problem starting a login shell since busybox does not accept the -l option. Use the dot . command to source commands from your profile(s) before you do the exec eg

/bin/sh -c "cd /mnt/flash;. /etc/profile;exec /bin/sh"
  • This almost works for me. The problem is I need a login shell. If I run this without the - at the beginning it works, but it's not a login shell which causes new problems. If I run it like this: ::respawn:-/bin/sh -c "cd /mnt/flash;exec /bin/sh" I get this error: /bin/sh: exec: line1: -/bin/sh: not found
    – Dakkaron
    Jun 30 '15 at 11:42
  • does your shell accept the -l or --login option this will make it a login shell. read man sh for your system, you haven't specified which shell you are running.
    – X Tian
    Jun 30 '15 at 11:46
  • I tried that, but I get the same result as without the -l and without the - in the beginning. I am running busybox sh.
    – Dakkaron
    Jun 30 '15 at 12:02
  • 1
    This was particularly helpful for me, where the use case was starting an interactive shell inside a docker container without having to manually cd into a long path every time.
    – rsethc
    Apr 12 '20 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.