
- Kali Linux Tutorial
- Kali Linux - Home
- Installation & Configuration
- Information Gathering Tools
- Vulnerability Analyses Tools
- Kali Linux - Wireless Attacks
- Website Penetration Testing
- Kali Linux - Exploitation Tools
- Kali Linux - Forensics Tools
- Kali Linux - Social Engineering
- Kali Linux - Stressing Tools
- Kali Linux - Sniffing & Spoofing
- Kali Linux - Password Cracking Tools
- Kali Linux - Maintaining Access
- Kali Linux - Reverse Engineering
- Kali Linux - Reporting Tools
- Kali Linux Useful Resources
- Kali Linux - Quick Guide
- Kali Linux - Useful Resources
- Kali Linux - Discussion
How to substitute an environment variable using sed on Linux?
An Environment variable is a dynamic-named value. Generally, these are variables are exported in a single terminal using a command shown below
export $SOMEVARIABLE=value
or they are stored inside the bash files, so that they remain available in all the terminals once the file is sourced. If we want to change an environment variable we can simply change the above command’s value field and then source the file, but if we want to do that with the help of the sed command then we need to know a little bit about the sed command.
Let’s explore the sed command, which is short for stream editor. This command is used to perform different functions like find, replace, insert and many more on a particular file.
Syntax
sed [OPTIONS]... [SCRIPT] [INPUTFILE...]
Example 1
Considering we have a simple text file where we want to replace a string that is present in the file to another string, we can make use of the grep command in that case.
File
immukul@192 linux% cat somefile.txt this is a test file and it is used for testing and is not available for anything else so please stop asking, lionel messi
Now we want to replace the string “it” with “litt” in the above file. The command to do that is shown below −
Command
sed ‘s/it/litt’ somefile.txt
Output
immukul@192 linux% cat somefile.txt this is a test file and litt is used for testing and is not available for anything else so please stop asking, lionel messi
Now let’s see how to exchange an environment variable’s value inside a bash script with the help of the sed command.
There’s an environment variable TUTS whose value is equal to /Users. In order to change that value in a bash script, we can add these lines to the script.
Script
sed 's@PPP@'"$TUTS"'@'
Now if we run the above script, and type PPP, then instead of printing PPP back, we will be able to print the value of the environment variable.
Output
immukul@192 linux-questions-code % ./sss.sh PPP /Users
- Related Articles
- How to set Python environment variable PYTHONPATH on Linux?
- Guide to the sed Stream Editor on Linux
- How to configuring Java Environment on Linux?
- Delete expoted environment Variable in Linux shell
- Text and File processing using sed Linux Commands
- How to get environment variable value using PowerShell?
- How to set the GOPATH environment variable on Ubuntu?
- How to set python environment variable PYTHONPATH on Windows?
- How to set python environment variable PYTHONPATH on Mac?
- Setting MySQL Environment Variables on Linux
- What is an Environment Variable in Postman?
- What is the sed in-place flag that works both on Mac and Linux?
- How to get the value of environment variable in Postman?
- Managing Environment Variables in Linux
- Locale Environment Variables in Linux
