what is the use of parentheses in linux command [duplicate] - linux

This question already has answers here:
What does it mean in shell when we put a command inside dollar sign and parentheses: $(command)
(2 answers)
Closed 3 years ago.
I run following command in Linux terminal. Can anyone tell me what is the use of parentheses in Linux terminal and following command also ?
$(echo "GET / HTTP/1.0";echo "Host: www.google.com"; echo) | nc www.google.com 80

( list )
Placing a list of commands between parentheses causes a subshell environment to be created, and each of the commands in list to be executed in that subshell. Since the list is executed in a subshell, variable assignments do not remain in effect after the subshell completes.

Parentheses denote a subshell in bash. In your command, the $() is command substitution and if it is like () is a subshell. Both of them run commands, the difference is what happens to the output.
Unix & Linux Answer

Related

How to execute text from stdout in bash [duplicate]

This question already has answers here:
Bash script - variable content as a command to run
(7 answers)
Closed 1 year ago.
So I have a command that's like
cmd1|cmd2|...|cmdN|execute
The output of cmdN is one line that I want execute to execute as if I copy pasted the output of cmdN into the terminal myself. I've tried to replace execute with bash, with $, and I tried to use xargs. (I'm still kinda confused on each of the options I've tried).
What's the simple answer here that's gonna make me wanna delete this post?
If you need to execute it in the current shell, use the eval command:
eval "$(cmd1|cmd2|...|cmdN)"
If it can be executed in a subshell, pipe to bash:
cmd1|cmd2|...|cmdN | bash

How to fix command not found in Linux shell scripting [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
why subtraction return - symbol
(2 answers)
Closed 2 years ago.
I have a Linux shell script with the below code
#! /bin/bash
echo $BASH
name = Mark
echo $name
When I run the script, I'm getting an error:
./my_script.sh: line 3: =: command not found
What am I doing wrong?
Note: I'm using Kali Linux.
In shell, you need to write:
echo $BASH
name=Mark
echo $name
Note there are no spaces around = when setting a variable. The shell usually interprets name = Mark as calling the command name with arguments =and Mark, hardly what you intend. It also seems that name somehow expands to nothing (an alias?), thus the confusing message about command =.

Unable to print the public ip in shell script [duplicate]

This question already has answers here:
What is the difference between $(command) and `command` in shell programming?
(6 answers)
Closed 4 years ago.
Hi I am trying to print the public ip of the machine in a file using shell script. I am using the command
ip=${curl ipinfo.io/ip}
in my script file and it gives an error saying bad substitution. whereas this command works when i run it in command line.
Is this the right way to get the ip through the script?
Thanks in advance!!
You are capturing the result of program so you should use $(). The following should work for you (with the -s parameter to curl stopping unnecessary output)
ip=$(curl -s ipinfo.io/ip)

Changing bash prompt temporarily by own script [duplicate]

This question already has answers here:
Changing PS1 prompt in a Bash parent shell
(3 answers)
Closed 6 years ago.
I wanted to write a tiny shell script that shortens the commmand prompt when it gets too long. Setting the PS1 variable in bash works fine. When I try the same command directly in a script and run it, nothing happens.
#!/bin/bash
PS1='\u:\W\$ '
I tried eval "PS1='\u:\W\$ '" , export PS1='\u:\W\$ ' and exec PS1='\u:\W\$ ' without any result.
How can I achieve the same result as a direct input in the bash?
Thank you in advance
In general, in UNIX, a process can only change variables for itself and its children -- not its parent ("its parent" being the process that invoked it).
You need to source a script, not execute it, for it to be able to effect your interactive shell's variables. This executes all commands inside the script inside your current shell, not a new shell started as a child process (whose variables' values are thrown away on exit).
# in bash
source yourscript
# or in POSIX sh
. yourscript # mind the space!
In this usage, the shebang does nothing; similarly, the +x permission isn't needed either. It's also typical to name scripts intended to be sourced rather than executed with an extension mapping to the shell they're intended to be used by (yourscript.bash for bash, yourscript.sh for a script which can be sourced by any POSIX shell), whereas scripts intended to be executed rather than sourced should have no extension.

Bash foreach loop works differently when executed from .sh file [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 9 years ago.
The following 'oneliner' does what I need:
$ for i in {1..5}; do echo $i; done
1
2
3
4
5
However, when I place exactly the same code into for-each.sh file and execute it, I get different result. Why?
for-each.sh file:
#!/bin/bash
for i in {1..10}; do echo $i; done
Result after execution:
$ ./for-each.sh
{1..10}
EDIT
Uf. I'm sorry. Now I noticed that I executed the for-each.sh by sh ./for-each.sh command and not by ./for-each.sh. I didn't know the difference between bash, sh, dash, ... After reading stackoverflow.com/a/5725402/915756 I realized that I executed the file by dash which points to /bin/sh by default on my Debian machine.
If you're confident that it is indeed bash executing your script, you can explicitly turn on brace expansion (expansion of {...} expressions) as follows:
set -B # same as: set -o braceexpand
Make this your script's first command after your shebang.
(Conversely, set +B (set +o braceexpand) would turn brace extension OFF.)
Conceivably, your system is configured to have brace extension turned off by default.

Resources