Results 1 to 2 of 2

Thread: Bash error 1: command not found

  1. #1
    Join Date
    Jan 2018
    Beans
    3

    Smile Bash error 1: command not found

    There is an Error in
    Code:
    ch=$ch$($i)
    in the following code:


    Code:
    #!/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; January 25th, 2018 at 09:28 AM.

  2. #2
    Join Date
    Dec 2014
    Beans
    1,341

    Re: Bash error 1: command not found

    '$( ... )' is a command substitution, so $($i) substitutes the value for the variable and tries to execute it and put it's output into the command. What you probably want to do is a variable indirection.

    And the literal string 'ch' is never going to be equal to an empty string (and neither is $ch, the value of the variable ch). You probably want to compare $i to the number of positional parameters.

    Search the bash manual page for the underlined terms ...

    And if you want to simplify the script, you might want to take a look at the bash-built-in command 'shift'

    Holger
    Last edited by Holger_Gehrke; January 25th, 2018 at 12:50 PM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •