Questions tagged [assignment]
The assignment tag has no usage guidance.
35
questions
0
votes
0
answers
20
views
Completely disable NetworkManager from using DHCP client?
I am using RHEL 7.9.
This version of RHEL I use nmcli and nmtui to configure network connections.
Problem:
NetworkManager service appears to be filling up journalctl logs with the following output
Apr ...
6
votes
2
answers
136
views
why is zsh not expanding this glob defined inside a function '(:a)'
The second line of the script only works if I trigger glob expansion by executing echo. I can't understand why. Here's the command and it's execution to give some context.
Function definition:
~/ cat ~...
0
votes
2
answers
1k
views
How to move home using relative path [closed]
The question is "Your current directory is cambridge. Move to your home directory using a relative pathname (don't use cd by itself for this move)" but I cannot for the life of me figure it ...
0
votes
0
answers
41
views
Variable assignment echo $VAR in subshell is a good method or not? (Nested bash functions)
I'm not a BASH programmer and whenever I have to create scripts it's a lottery for me, so apologies if my script looks too redudant or written by a newbie.
As a foreword I have many screen snapshots ...
0
votes
1
answer
70
views
one line definition of a variable conditional on the output of a logical test
In bash programming I've always defined conditional variables in the following long hand way
dog=1
if [[ $dog -eq 1 ]] ; then
cow=1
else
cow=0
fi
This is obviously incredibly inefficient. In ...
3
votes
2
answers
690
views
changing IFS temporarily before a for loop [duplicate]
I know that the SHELL allows variable assignment to take place immediately before a command, such that IFS=":" read a b c d <<< "$here_string" works...
What I was ...
1
vote
1
answer
155
views
Result of a command is not assigned to a variable
I am pretty unexperienced (to put it mildly) when it comes to BASH and Shell scripting, so bear with me:
$ toda=$(date) | echo $toda
gives me: Fr 29 Mai 2020 15:25:19 CEST. So far so good.
datediff ...
0
votes
3
answers
2k
views
Assign to a variable in a bash script
I have the following script and I try to create an empty text file. I tried touch and I tried echo -n > - they both fail.
#!/bin/bash
set filename_result="contents"
echo "filename=$...
4
votes
1
answer
103
views
$-expansions not performed as expected when following a redirection
It seems that bash and zsh will perform the variable and arithmetic expansions in a child process when
a) they're following a redirection operator like <, >, >> or <<<.
b) the ...
8
votes
5
answers
6k
views
Assign the same string to multiple variables
I want to assign the string contained in $value to multiple BASH variables.
Actually the example I gave before (var1=var2=...=$value) was not reflecting exactly what I wanted.
So far, I found this but ...
-1
votes
2
answers
880
views
Bash String Manipulation and Assignment
How can I manipulate a string and then assign it to a variable?
This string manipulation works as I want:
echo ${dir:2:5} | sed 's/[.]$//';
but:
var x = ${dir:2:5} | sed 's/[.]$//';
and
x = ${...
4
votes
4
answers
2k
views
Assign variable using multiple lines
I have a function
f(){
echo 777
}
and a variable to which I assign the "return value" of the function.
x=$(f)
Very concise! However, in my real code, the variable and function names are quite ...
1
vote
1
answer
43
views
What options are available to copy date output to a variable? [closed]
Context: router outfitted with Merlin AsusWRT. The goal is to append the epoch time to a text file from the UNIX command-line within router. Example:
{date:1532348491}
The date command return ...
-5
votes
1
answer
51
views
Why is unquoted $PATH containing whitespaces safe in an assignment? [duplicate]
What does the following from https://unix.stackexchange.com/a/154290/674 mean?
In general, in shells other than zsh, $PATH outside double quotes breaks when the value contains spaces or other ...
7
votes
1
answer
281
views
Unexpected outcome of a="$@"
I'm struggling with this situation:
$ set -- 1 2 3
$ a="$@"
$ echo "$a"
1 2 3
What I find unexpected is the assignment itself.
man bash says this about the "$@" expansion:
When the expansion ...
3
votes
1
answer
884
views
Bash: space as a key in an associative array
This code in Bash
s="There are seven pencil"
declare -A A
while IFS= read -rn1 a; do
[ -z "$a" ] || [ -n "${A[$a]}" ] && continue
printf %s "$a"
((A[$a]++)) # A[$a]=x
done <<<"$...
4
votes
4
answers
2k
views
Why is foo=bar different from foo= bar? [duplicate]
What is the interpretation of the whitespace in this command foo= bar ?
Why are foo=bar and foo= bar interpreted differently
Example (Ubuntu bash)
developer@1604:~$ foo=bar
developer@1604:~$ foo= ...
2
votes
2
answers
333
views
Why is it not required to double quote `$bar` in assignment `foo=$bar`? [duplicate]
From https://unix.stackexchange.com/a/32227/674
The main places where it's safe not to use the double quotes are:
in an assignment: foo=$bar (but note that you do need the double quotes in ...
11
votes
2
answers
1k
views
Assignments are like commands with an exit status except when there's command substitution?
See the following examples and their outputs in POSIX shells:
false;echo $? or false || echo 1: 1
false;foo="bar";echo $? or foo="bar" && echo 0: 0
foo=$(false);echo $? or foo=$(false) || ...
-3
votes
1
answer
332
views
Lexical analysis for assignment in a POSIX shell
For the lexical analysis in a POSIX shell, from http://pubs.opengroup.org/onlinepubs/007904875/utilities/xcu_chap02.html
7. [Assignment preceding command name]
a. [When the first word]
...
11
votes
2
answers
15k
views
One line env variable set behavior [duplicate]
If I do this:
SUMAN_DEBUG=foo echo $SUMAN_DEBUG
I get no output
but if I do:
SUMAN_DEBUG=foo && echo $SUMAN_DEBUG
then I get
"foo"
why is that?
0
votes
3
answers
2k
views
Bash - Set a variable [duplicate]
I need to set a variable in my bash script
#!/usr/bin/env bash
GITNAME= git config --global user.name
echo " $GITNAME "
But it doesn't seems to work that way.
How does it work?
1
vote
1
answer
74
views
Output in variable shouldn't be interpreted as command [duplicate]
So this question was asked some times already, but the answers didn't seem to work for me. So I have this simple script
#!/bin/bash
charon_id = $(pidof charon)
kill -1 $charon_id
And console says ...
1
vote
1
answer
172
views
Different command outputs
I entered same command with a variable host. During 2nd time I entered blank space between host,= and $. Why it was trying to connect to a server ? Also, when I write like $(host name), I am getting ...
31
votes
3
answers
15k
views
Spaces in variable assignments in shell scripts
What is the difference between below variables assignments?
var=23
var =23
var= 23
var = 23
Is there any difference in space around the assignment operator?
2
votes
1
answer
150
views
How can I run speech to text and save the result in a variable?
I would like to speak into my computer's microphone, have what I say converted to text and then have that available as a shell variable.
Is this possible? I thought I might do it using Google's ...
5
votes
1
answer
3k
views
Any problem assigning one variable to another in shell without using quotes? [duplicate]
This question is about assigning the entire contents of one variable to another variable.
Variable is not being used (sent to echo, etc.)
No parameter expansion is being done during the assignment.
...
3
votes
3
answers
1k
views
What is the difference of defining a variable with or without quotation marks? [duplicate]
If I define a variable with the quotation mark:
TEMP="~/Dropbox"
then
ls $TEMP
would not work, instead
echo $TEMP | ls
works.
And to get the same result, I can also define the variable without ...
15
votes
2
answers
48k
views
Zsh: export: not valid in this context
When running this script, I run into an error on this line (relevant snippet below):
...
_NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@")
if (( $? == 0 )); then
export PATH=$_NEW_PATH
# If ...
1
vote
1
answer
149
views
create an array
I want to learn about arrays and how to assign values to them, so I found this tutorial
While running the following script:
#!/bin/bash
$names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad John")...
2
votes
0
answers
95
views
How do I assign a value to the Path key in a .desktop file including the $HOME environment variable correctly?
I'm making a .desktop file for Minecraft. Nothing appears to happen upon executing the it. The Exec and Path keys in the file:
Exec=java -jar Minecraft.jar
Path=$HOME/.minecraft/
I've also tried ...
6
votes
2
answers
4k
views
How do I write a command for the Exec key in a .desktop file containing a reserved character correctly?
I'm trying to make a .desktop file for Minecraft. Nothing appears to happen upon executing the file. I've tried assigning the Exec key as follows:
Exec= java -jar "~/.minecraft/Minecraft.jar"
Exec= ...
10
votes
2
answers
6k
views
Variable assignment outside of case statement
In many languages it is possible to assign the result of a case/switch statement to a variable, rather than repeating the variable assignment many times within the case statement. Is it possible to ...
48
votes
2
answers
10k
views
Are quotes needed for local variable assignment?
Can I safely omit quotes on the right side of a local assignment?
function foo {
local myvar=${bar}
stuff()
}
I'm mainly interested in bash, but any info on corner cases in other shells are ...
22
votes
3
answers
56k
views
Conditional assignment based on environment variable
In a bash script, I'm assigning a local variable so that the value depends on an external, global environment variable ($MYAPP_ENV).
if [ "$MYAPP_ENV" == "PROD" ]
then
SERVER_LOGIN=foobar123@prod....