1

I have a folder in ~ called work that contains all my work. Usually when I start an instance of Terminal I type cd work.

I would like to avoid this because it's very repetitive. I tried putting cd work in my .bashrc but I realized that I don't always want to cd work - for instance, when I Open Terminal here or when I open a Terminal in VSCode (which should open my Terminal in the current project directory, not work).

My idea is having the Terminal type out cd work whenever I open it, but not actually execute the command. If I really need to cd work, all I have to do is hit Return. However, if I don't want to do that, I can just Ctrl+C.

Is doing this possible?

a3y3
  • 113

1 Answers1

1

Solution

You can use the bash built-in command read:

  • Either use it in a simple one line and add it to the end of your ~/.bashrc file like so:

    read -p "${PS1@P}cd work"; cd work
    
  • Or define a custom function and call it at the end of your ~/.bashrc file like so:

    myfunction() {
        mycommand="cd work"
        read -p "${PS1@P}$mycommand"
        $mycommand
    }
    

    myfunction

Information

  • read -p will show the text and wait for either Enter to continue or Ctrl+c to abort.
  • ${PS1@P} will show your current prompt before the text.
Raffa
  • 34,963