-1

I assume that this question was asked many times but i didn't find suitable solution. Every time when i open my shell i need to execute commands to open my working directory enable virtualenv and open file. For example

cd storage/predictions #open project directory
. env/bin/activate  #activate virtual environment 
cd forecast  #open directory
vim file.py    #open a file 

am i can to execute above commands in more convinience mode such as nest these several commands in one. For example

cd storage/predictions & . env/bin/activate & cd forecast & vim file.py 

Thank in advance

  • you need to write a function, so please refer to [In Bash, when to alias, when to script, and when to write a function?](https://unix.stackexchange.com/questions/30925/in-bash-when-to-alias-when-to-script-and-when-to-write-a-function) for more information – αғsнιη Nov 11 '19 at 12:43
  • In principle, your example would already do the job, albeit not in the comfortable way you desire. However, you will have to replace the single `&` with double `&&`. – AdminBee Nov 11 '19 at 12:46
  • You're welcome. Still I think you should consider @αғsнιη 's suggestion to look into shell scripting if you want to automate more complex tasks in the future. Later, you may also want to check out the [Advanced Bash Scripting Guide](https://www.tldp.org/LDP/abs/html/). – AdminBee Nov 11 '19 at 12:52

1 Answers1

0

If you are going to change your environment, you need to "source" the command, not just run a script. That's because a child script is never permitted to change its parent's environment.

Sourcing (dot files) are mainly used for shell start-up, but there is nothing to stop you using them locally.

Make a file called something like "myEnv"in your home directory, containing those commands. You don't need the directory to be on your path, and the file does not need to be executable. You could have several for different start-ups.

First thing when your terminal opens, type: . myEnv

I used to keep a vi session open in a small window, for a file in my home directory called Help. I pasted any frequently-used or complicated commands in there, and saved it occasionally. Just post-its as reminders, and to eliminate typos.

Paul_Pedant
  • 8,228
  • 2
  • 18
  • 26