Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

I write a shell in linux to delete - in string, why it doesn't work? a="23-45-67" a=$a…

  1. User

    I write a shell in linux to delete - in string, why it doesn't work? a="23-45-67" a=a∣sed′s/−//g′echoa | sed 's/-//g' echo a

  2. Assistant

    The issue with the script is that the pipe (|) operator is used instead of command substitution ($()), causing the output of the sed command to be assigned to stdout and not the variable a.

    The corrected script would be:

    a="23-45-67"
    a=$(echo $a | sed 's/-//g')
    echo $a
    

    This would replace all occurrences of - with nothing in the string and print the result, which would be 234567.

  3. User

    but it still doesn't work.

  4. Assistant

    In that case, there might be a syntax error in your script.

    Here's an updated version of the script that uses command substitution instead of piping the output to the variable:

    a="23-45-67"
    a=$(echo $a | tr -d '-')
    echo $a
    

    This script uses the tr command to remove all occurrences of - from the string.

    The output of this script should be:

    234567
    

    This approach should work as long as your version of Linux has the tr command installed.

  5. User

    How to output echo to a variable inlinux

  6. Assistant

    To store the output of an echo command in a variable in Linux, you can use the command substitution syntax: $(command).

    Here's an example:

    my_variable=$(echo "Hello, world!")
    

    This will store the string "Hello, world!" in the variable my_variable.

    You can then use the echo command to print the contents of the variable:

    echo $my_variable
    

    This will output:

    Hello, world!
    

    If you want to store the output of a command in a variable without printing it to the screen, you can simply omit the echo command and use command substitution:

    my_variable=$(ls)
    

    This will store the output of the ls command (i.e., a list of files in the current directory) in the variable my_variable.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.