ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#!/bin/bash
origclip="open documents open music"
i=0
set -- $origclip
ch=$1
while [[ ch != "" ]]; do
((i++))
ch=$ch$($i) #this does not execute even ch=$($i) doesn't execute
echo $ch
done
I want to achieve following output.
Code:
open
open documents
open documents open
open documents open music
new line and space are not required, presence of space don't matter in output.
In short I want to achieve output of following code, with a variable 'i' which increments as in previous code
Code:
origclip="open documents open music"
set -- $origclip
echo $1
echo $1$2
echo $1$2$3
echo $1$2$3$4
Also there is a problem in achieving end of line
Code:
while [[ ch != "" ]]; do
i am not sure of this.
Last edited by pankajk2526; 01-25-2018 at 02:27 AM.
It is not clear what your actual goal is, but that is a hard way to go about getting what you have described.
You may have some reason for setting the values of positional parameters, but if not something like this will do what you have described:
Code:
origclip="open documents open music"
for wd in $origclip; do
ch="${ch}${wd}"
echo "$ch"
done
The reason for the error in your example code is this...
Code:
ch=$ch$($i) #this does not execute even ch=$($i) doesn't execute
$($i) attempts to execute $i, so the command does not exist. Were you thinking that this was equivalent to an array subscript?
The problem with detecting the end of the string is that you are testing whether "$ch" is empty, when you continue to append more to it - so it is never empty.
You need to read up on how shell scripting works.
I highly recommend the bash man page as an excellent place to get the details - well organized and well written (uncommon among man pages!).
Yes, $($i) runs the contents of i as a command. Not wanted here.
For a space-separated list a for loop is more appropriate than a while loop.
Code:
origclip="open documents open music"
set -- $origclip
ch=""
for c; do
ch=$ch$c
echo "$ch"
done
The for takes the positional parameters as default.
Perhaps you do not even need to set the positional parameters?
This was suggested before. Here is how to add a space separator in the output.
Code:
origclip="open documents open music"
ch="" sep=""
for c in $origclip; do
ch=$ch$sep$c
echo "$ch"
sep=" "
done
Last edited by MadeInGermany; 01-27-2018 at 01:12 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.