Timeline for Are variables like $0 and $1 shell/environment variables?
Current License: CC BY-SA 3.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Oct 26, 2017 at 1:54 | comment | added | Sergiy Kolodyazhnyy | @user7681202 updated my answer. Let me know if you have any other questions. | |
| Oct 25, 2017 at 23:11 | comment | added | Sergiy Kolodyazhnyy | @user7681202 technically speaking, yes there are ways to do that. bash manual shows that syntax for bash is bash [options] [command_string | file] , so if you have a script files called Hello and World you can easily do gnome-terminal -e 'bash Hello World' , and bash will execute them, because that's how it's built to understand positional parameters. It won't treat them as text strings. BUT you could also do hello=world gnome-terminal -e 'bash' and you can access $hello variable from environment. It's a convoluted topic, and I'll add more details on it to my answer later | |
| Oct 25, 2017 at 23:01 | comment | added | user7681202 | @Sergiy Kolodyazhnyy You are right, I mixed these things. Is there a way to run a shell with arguments with a terminal, I tried the following inside a gnome-terminal (bash Hello World), but I got an error (it thought that I am running a script called "Hello"). | |
| Oct 25, 2017 at 22:50 | comment | added | Sergiy Kolodyazhnyy | @user7681202 you're confusing gnome-terminal with shell as Jaroslav explained. gnome-terminal is one program, shell is another. gnome-terminal runs shell. Arguments that you give to gnome-terminal will have those Hello and World as arguments to itself. And gnome-terminal will start shell with its own positional parameters. $1 and $2 are really meant for non-interactive shells, i.e. for scripts and things like bash -c 'commad 1; command 2' | |
| Oct 25, 2017 at 22:41 | comment | added | Jaroslav Kucera | @user7681202 The gnome-terminal isn't shell, it's terminal emulator (like xterm, konsole etc.). The shell runs inside the terminal and it can be bash/sh/zsh/tcsh and many more. The script is executable file with proper header (like #!/bin/bash) and content interpretable by the shell specified in the mask. It usually uses suffix .sh | |
| Oct 25, 2017 at 22:37 | comment | added | Jaroslav Kucera | @Jesse_b Thank you, I've added the content of $0 into the answer. | |
| Oct 25, 2017 at 22:36 | comment | added | user7681202 | I tried to run the gnome-terminal with arguments (gnome-terminal Hello World). I could see $0, but I could not see $1 and $2. | |
| Oct 25, 2017 at 22:36 | history | edited | Jaroslav Kucera | CC BY-SA 3.0 | Specified the $0 default content |
| Oct 25, 2017 at 22:33 | comment | added | jesse_b | @user7681202: Which ones can you see in your terminal? $0 will point to your current terminal process (likely bash) and $? is simply the exit code of the last process. | |
| Oct 25, 2017 at 22:30 | comment | added | user7681202 | "So when you're outside the script, these variables aren't available" What do you mean by "outside the script", because I can see the values of these variables in my terminal. | |
| Oct 25, 2017 at 22:19 | history | answered | Jaroslav Kucera | CC BY-SA 3.0 |