Questions tagged [assignment]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
-1 votes
1 answer
176 views

Zsh string variable containing multi words must be assigned to an array

How to have Zsh string variable containing multi words separated by space be assigned to an array so that each word is the array element s='run help behind me' a=($s) m=${a[0]} n=${a[1]} print "m=...
user avatar
1 vote
1 answer
82 views

How to iterate over a directory for files without including sub-directories in Bash

I need to parse through a directory that contains files and subdirectories however I am unable to use the find command. The for loop is supposed to go through each file and compare it to another ...
IMPNick's user avatar
  • 11
9 votes
2 answers
764 views

In POSIX scripting, is x=$y always equivalent to x="$y"?

Are x=$y and x="$y" always equivalent? Didn't know how to search for this. So far, I've always been using x="$y" to be "on the safe side". But I used x=$1 at one point ...
finefoot's user avatar
  • 3,080
-1 votes
1 answer
49 views

Grouping commands of if...then statement, including assignation of variable for shell/bash in a single line

I know the existence of while-do-done. Alternatively, I know that I can use for-do-done. But, I was trying to test manually using if-then-else. I would to like to execute a command, but doing a shift ...
joseluisbz's user avatar
0 votes
1 answer
349 views

Assigning $* in shell script from args array

After iterating thru the shell arguments and filtering out unwanted arguments into an array, I want to assign the array back to $*. For example, in the below script, I want to remove the arguments -f ...
user3118280's user avatar
1 vote
0 answers
1k 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 ...
Dave's user avatar
  • 601
7 votes
2 answers
549 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 ~...
bryan hunt's user avatar
0 votes
2 answers
2k 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 ...
AASFLC's user avatar
  • 3
0 votes
0 answers
87 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 ...
Alex's user avatar
  • 21
0 votes
1 answer
359 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 ...
Leo Simon's user avatar
  • 453
8 votes
2 answers
3k 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 ...
computronium's user avatar
1 vote
1 answer
693 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 ...
Guebert's user avatar
  • 13
0 votes
3 answers
5k 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=$...
andrewz's user avatar
  • 153
4 votes
1 answer
107 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 ...
user avatar
9 votes
5 answers
10k 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 ...
SebMa's user avatar
  • 2,179
-1 votes
2 answers
979 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 = ${...
user3783243's user avatar
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 ...
pfnuesel's user avatar
  • 5,847
1 vote
1 answer
106 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 ...
gatorback's user avatar
  • 1,384
-5 votes
1 answer
106 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 ...
Tim's user avatar
  • 102k
7 votes
1 answer
301 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 ...
user avatar
2 votes
1 answer
1k 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 <<<"$...
user avatar
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= ...
Niklas Rosencrantz's user avatar
2 votes
2 answers
389 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 ...
Tim's user avatar
  • 102k
12 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) || ...
phk's user avatar
  • 5,963
-3 votes
1 answer
429 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] ...
Tim's user avatar
  • 102k
15 votes
2 answers
21k 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?
Alexander Mills's user avatar
0 votes
3 answers
3k 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?
Kevin's user avatar
  • 219
1 vote
1 answer
84 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 ...
GreenThor's user avatar
  • 113
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 ...
abc's user avatar
  • 13
41 votes
3 answers
20k 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?
ajay's user avatar
  • 451
2 votes
1 answer
243 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 ...
venkatraman's user avatar
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. ...
Caleb Paul's user avatar
3 votes
3 answers
2k 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 ...
username123's user avatar
19 votes
3 answers
94k 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 ...
Amelio Vazquez-Reina's user avatar
1 vote
1 answer
157 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")...
Tanatos Daniel's user avatar
2 votes
0 answers
163 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 ...
reggie-man's user avatar
6 votes
2 answers
5k 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= ...
reggie-man's user avatar
11 votes
2 answers
7k 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 ...
iconoclast's user avatar
  • 9,208
50 votes
2 answers
12k 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 ...
rahmu's user avatar
  • 20k
26 votes
3 answers
73k 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....
Jonik's user avatar
  • 1,500