Linked Questions

17
votes
2answers
21k views

Modify global variable in while loop [duplicate]

I have a script that process a folder, and count the files in the mean time. i=1 find tmp -type f | while read x do i=$(($i + 1)) echo $i done echo $i However, $i is always 1, how do I ...
13
votes
3answers
4k views

Does while spawn a subshell in bash? [duplicate]

Can someone explain why this script doesn't produce the output I was expecting? #!/bin/bash # var=0 ls -1 /tmp| while read file do echo $file var=1 done echo "var is $var" I get a ...
3
votes
3answers
6k views

Why is the array empty after the while loop? [duplicate]

I try to initialize an array in bash-4.2 next way: ring=() ls -las | tail -n +4 | while read line > do > ring+=("$line") > echo ${ring[-1]} > done 3924 -rw-r--r-- 1 username group 4015716 ...
4
votes
1answer
2k views

Bash: Variable assignment doesn't seem to 'stick' [duplicate]

I am having a hard time tracking down the reason my 'boolean' flag variable won't stay false when a test fails inside a while loop. The script involves a few loops but essentially it is intended to ...
1
vote
1answer
4k views

bash adding 1 to variable in while loop [duplicate]

The situation is as following: I have a .txt file that includes series of letters all divided bij newlines Here's a small part of the file: lrtjxz lrtjhs lrtsxv lrtvsw lrtywj lrtyws lrxwwv lrvhsx ...
1
vote
1answer
581 views

Adding to arrays [duplicate]

I have this script tests=() igrepy -l $1 . | while read -r line do // some processing tests+=("${second[0]}") done echo ${tests[@]} I've checked that 'second' has a result and it does, ...
1
vote
1answer
361 views

Bizarre variable behavior in `while` loops [duplicate]

Can someone explain to me what's going on here? Script: #!/bin/sh SKIP="unity-launcher|unity-panel|unity-dash|Hud|XdndCollectionWindowImp|Desktop" WINS=() wmctrl -l | grep -Ev " (${SKIP})" | cut -d \...
2
votes
1answer
503 views

Variable inherited in the subshell without exporting in the main shell [duplicate]

I am having the below shell script var="this is a test" ls -ltr| while read file do echo $var done echo $var I am getting the below output: this is a test this is a test this is a test How ...
1
vote
2answers
775 views

Can't pipe from echo to bash built-in read? [duplicate]

Say I have a loop that goes through a file line-by-line via read. I would like to have both the original line, and the line split into different variables, which read does nicely. However, I can't ...
1
vote
1answer
733 views

Print out variables from subshell to parent shell [duplicate]

Very new to Bash and pretty confused about local/global variables/subshells. I'm not sure why the modified variables won't print out at the end of the function—I'm trying to print out a final line ...
1
vote
1answer
112 views

bash script var not changed after the loop [duplicate]

I'm quite new to bash scripting, and I wrote the following script to emulate the wc -c command: (I know it doesn't count the end of the line) #!/bin/bash echo $1 len=0 cat $1 | while read line do ...
0
votes
1answer
35 views

How can I read pathnames in a file line by line, and apply pushd to each pathname? [duplicate]

I have a file /tmp/pathnames where each line is a pathname to a dir. /tmp/my dir1 /tmp/dir2 /tmp/dir3 I want to apply the command pushd to each line in the file, so that I can have those pathnames ...
1
vote
1answer
48 views

Why can't I use read -a to break a string into words? [duplicate]

I have a sample input file called in with the following contents: m@m-X555LJ:~/wtfdir$ cat in asdf1 jkl1 asdf2 jkl2 asdf3 jkl3 I now execute: m@m-X555LJ:~/wtfdir$ mapfile < ./in As expected, ...
0
votes
0answers
39 views

unable to access value of variable outside loop to get count [duplicate]

I want the count to be displayed after the loop is executed but to do so when I write echo $count it displays 0. So the issue is that I am unable to access the value of variable outside the loop. #!/...
0
votes
0answers
35 views

question about a 'while' logics error [duplicate]

Possible Duplicate: Why is my variable being localized in one ‘while read’ loop, but not in another seemingly similar loop I have a bash script that scraps URLs to feed a MySQL database. In my ...

15 30 50 per page