0

When I execute bash using bash, I want a custom script to run, in that bash.

I want that script to always be the same script, and not provided via argument. Just a default custom initialization for my bash.

I couldn't find how to do that. This is not the same, nor is this.

I want the script to run when bash starts, not on startup of linux.

Gulzar
  • 135
  • 2
    When bash starts how? If you run a bash script (i.e. a non-interactive bash shell)? If you start a new interactive login shell? A new interactive non-login shell? If you ssh into a machine? All of these use cases are different and require different approaches. Do you only want this to happen when you manually run the bash command with no other options (so when you start a non-login interactive shell)? – terdon Feb 23 '23 at 12:28
  • @terdon I don't know how to answer that. I start bash when I just type in bash. I don't even know what I type it into. I guess that is a non-login interactive shell. – Gulzar Feb 23 '23 at 19:00

1 Answers1

2

It seems that you need to use bashrc functionality. Everything placed in ~/.bashrc file will be executed during bash startup if bash is being run as an interactive, non-login shell.

As your use case is not clear, and there are some differences between using bash in different modes, I suggest you read more about it in the manual: https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html

terdon
  • 242,166
DevilaN
  • 1,966
  • great this is what I needed, just wanted my docker to cd and activate a python virtual env when I start it with a bash entrypoint – Gulzar Feb 23 '23 at 12:31
  • @Gulzar doesn't docker run a non-interactive, login shell? If so, this might work on some Debian based systems, because Debian's .profile sources .bashrc but this is absolutely not standard and cannot be relied upon. – terdon Feb 23 '23 at 12:33
  • @terdon I wrote a .bashrc file, copied it into ~/bachrc on docker startup, and that's it. When running it, the entry point is bash. Is it not good? – Gulzar Feb 23 '23 at 12:41
  • @Gulzar it depends on what operating system you or maybe your docker image is based on. I don't know enough about docker to be sure. If this is indeed running the command bash, then that should start an interactive, non-login shell, in which base .bashrc is the right file to use, yes. – terdon Feb 23 '23 at 12:44
  • Ok, something is off. I have the line source "$(pwd)/venv/bin/activate" in the .bashrc. When running directly from bash i works, but on bash startup: : No such file or directoryetadata/venv/bin/activate. What is wrong? – Gulzar Feb 23 '23 at 12:53
  • @Gulzar this is the kind of detail you need to have in the question. You didn't even mention docker there and the devil's in the details with this sort of thing. So please edit your question and clarify what you are doing. But there's never any point in using $(pwd)/directory, just use directory or ./directory. – terdon Feb 23 '23 at 13:33
  • @terdon good point regarding pwd, it solved it. The docker is, in fact irrelevant to the question, as the same happens with or without it, and it is a redundant layer of complexity to the minimal problem. The extra problem here was for some reason the .bashrc lines ended with \r. I passed it through dos2unix and that fixed it, when the file is one-line-long. More lines and problems happen, but this will do for now. – Gulzar Feb 23 '23 at 19:02
  • Ah. Yes, that suggests that at some point, you opened the file in Windows, @Gulzar, and that broke it since Windows has its own line endings (\r\n) instead of the standard (in the rest of the computing world) \n. – terdon Feb 23 '23 at 19:24
  • @terdon Great, thanks. asked GPT "how to change pycharm line endings" and that fixed it finally. – Gulzar Feb 23 '23 at 19:34