Lädt...


🔧 Back to bash: Inception, running bash inside bash


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

This post will be a shorter one that goes into different ways of running bash scripts inside bash (interactive shell or another bash script). While in general it is a fairly simple process, there are some useful things to keep in mind. At first i wasn't sure if it warranted it's own post, but I've been meaning to make my blogs a bit shorter, and I'm happy how it turned out in the end.

There's essentially two main ways of running another bash script inside bash (spoiler, there is a third way, but i suspect you won't use it as often).

Running a script by specifying the path

First things first, any script you try to run directly must have executable permissions. This is not the default when creating files, so you must remember to add them. As an example, granting only the user that owns the file access to execute it can be done using chmod u+x a.bash.

To run another bash script, simply specify the path, as in ./a.bash, to run it in the current working directory. Note that this is no different from how you would run any other program. Also note that the path to the script/executable must be given, hence the ./ prefix. Bash will do a few things under the hood:

  1. Prepare a new execution environment for the command. This contains a couple of things 1, the most important of which are the current working directory and the environment variables. These are inherited from the parent execution environment.
  2. Run the script into a new bash process with the newly prepared execution environment. Any changes the script makes to its environment do not affect the parent. As an example, consider the following script that sets an environment variable (export)
export ABC="42"
cd ../

When executed, we can observe that the ABC variable is not available in the environment of the parent, nor is the working directory altered.

~/some_dir/other_dir$ ./a.bash 
~/some_dir/other_dir$ echo $ABC

Sourcing the script

The source builtin, as in source a.bash or . a.bash (the first is just an alias for the latter), can be used to execute all commands from a given script in the current execution environment, allowing you to effectively run scripts that alter the environment and the working directory. The example from above is shown again, but this time it affects the shell.

~/some_dir/other_dir$ source ./a.bash 
~/some_dir$ echo $ABC
42

Running scripts from PATH

Remember how i said the executable must be specified as a path? That is because, when bash does not see a path (no slashes), it will try to look for an executable in any of the PATH directories. This can be desirable if you want to make a script readily available from any location. For example, if you add the a.bash file from before to /usr/local/bin, you can run it, like any other program, using its name, a.bash.

A final remark

At the very top of a file you want to execute, you can specify what is called a shebang 2. This will be picked up in most Unix systems, and indicates what interpreter should be used to run the script.

#!/usr/bin/bash
...

In fact, it is good practice to put this in shared executable scripts as it ensures that bash will be used even if someone tries to run it in a different shell.

You can also use this to run scripts in other interpreters, such as python, so that you can use some_script.py rather than python some_script.py. If you then drop the .py extension, you can obfuscate the fact that it's running python entirely.

  1. Among others, the execution environment also includes aliases and signal traps. For a full overview, check the bash manual or man bash help pages. ↩

  2. Apparently this is a contraption of the 2 characters used, I never would have guessed that though...  ↩

...

🔧 Back to bash: Inception, running bash inside bash


📈 70.12 Punkte
🔧 Programmierung

🎥 Bash Scripting - BASH Scripts For Heavy Commandline Users [Bash Snippets]


📈 22.53 Punkte
🎥 IT Security Video

🎥 Bash Scripting - BASH Scripts For Heavy Commandline Users [Bash Snippets]


📈 22.53 Punkte
🎥 IT Security Video

🐧 niieani/bash-oo-framework: Bash Infinity is a modern boilerplate / framework / standard library for bash


📈 22.53 Punkte
🐧 Linux Tipps

🎥 Docker Container Security - Vulnerable Upon Inception - ESW #167


📈 22.47 Punkte
🎥 IT Security Video

📰 Meta Quest: Hackangriff wie im Film "Inception" legt alle Daten offen


📈 22.47 Punkte
📰 IT Nachrichten

📰 Control gespielt: Max Payne meets Inception


📈 22.47 Punkte
📰 IT Nachrichten

📰 Inception Attack: Neue Angriffstechnik ermöglicht Manipulation von VR-Inhalten


📈 22.47 Punkte
📰 IT Nachrichten

📰 The Inception Bar: A New Android Chrome Phishing Method Disclosed


📈 22.47 Punkte
📰 IT Security Nachrichten

📰 Hitachi Vantara Pentaho+ allows organizations to oversee data from inception to deployment


📈 22.47 Punkte
📰 IT Security Nachrichten

📰 The Inception Bar: A New Android Chrome Phishing Method Disclosed


📈 22.47 Punkte
📰 IT Security Nachrichten

🔧 Infraception (Infrastructure + Inception) with CDK Pipelines


📈 22.47 Punkte
🔧 Programmierung

📰 The inception bar: a new phishing method


📈 22.47 Punkte
📰 IT Security Nachrichten

📰 "Inception": Handlung und Ende einfach erklärt


📈 22.47 Punkte
📰 IT Nachrichten

📰 The inception bar: a new phishing method


📈 22.47 Punkte
📰 IT Security Nachrichten

🔧 Peugeot Inception Concept: So stellt sich die Marke das Modell der Zukunft vor


📈 22.47 Punkte
🔧 Programmierung

📰 "Inception Attackers" Combine Old Exploit and New Backdoor


📈 22.47 Punkte
📰 IT Security Nachrichten

🔧 Peugeot Inception Concept: So stellt sich die Marke das Modell der Zukunft vor


📈 22.47 Punkte
🔧 Programmierung

📰 Inception Group Uses POWERSHOWER Backdoor in Two-Stage Spear Phishing Attacks


📈 22.47 Punkte
📰 IT Security Nachrichten

📰 Inception Concept: Peugeot zeigt Lenkrad im Getränkehalter-Look


📈 22.47 Punkte
📰 IT Nachrichten

📰 Attack inception: Compromised supply chain within a supply chain poses new risks


📈 22.47 Punkte
📰 IT Security Nachrichten

🔧 How python changed the Machine Learning Game since it's inception?


📈 22.47 Punkte
🔧 Programmierung

🐧 Linux users have had to deal with discrimination ever since its inception


📈 22.47 Punkte
🐧 Linux Tipps

📰 CES: CES: Stellantis zeigt Elektroauto-Studie Peugeot Inception Concept


📈 22.47 Punkte
📰 IT Nachrichten

🔧 The Evolution of JavaScript: From Inception to Modern Day


📈 22.47 Punkte
🔧 Programmierung

📰 Inception Project: Am Computex-Eingang geht Moddern das Herz auf


📈 22.47 Punkte
📰 IT Security Nachrichten

📰 "inception.py"... Multiple Base64 Encodings, (Fri, Jul 2nd)


📈 22.47 Punkte
📰 IT Security

🔧 Cognito Inception: How to add Cognito as OIDC Identity Provider in Cognito


📈 22.47 Punkte
🔧 Programmierung

📰 Inception and the Road from Security Serendipity


📈 22.47 Punkte
📰 IT Security Nachrichten

⚠️ Inception#5 – russisches Hacker-Magazin bei XSS.is erschienen


📈 22.47 Punkte
⚠️ Malware / Trojaner / Viren

📰 Heute im TV: Action-Comedy mit Starbesetzung - dieser Film schlug im Kino sogar "Inception"


📈 22.47 Punkte
📰 IT Nachrichten

📰 FBI: IC3 Received 6 Million Cybercrime Complaints Since Inception


📈 22.47 Punkte
📰 IT Security Nachrichten

matomo