1

I have to do an alias called for example "longest" for this script:

data=""; len=0; line=""; while [[ $line != "quit" ]]; do read line; [[ $line != "quit" ]] && [[ ${#line} -gt len ]] && len=${#line} data=$line; done; echo $len; echo $data 1>2

Its job is simply reading words or phrases and counting the characters.

I put the script inside quotes like this:

alias longest="...script..."

but it doesn't work and I don't know why. Can anyone explain to me why? A possible solution?

4
  • 2
    Why dont you simply write a script, make it executable and call it? Commented May 15, 2022 at 18:45
  • 1
    This absolutely should not be an alias. But, as a first attempt, try just using single quotes: alias longest=' ... ' Commented May 15, 2022 at 19:02
  • 5
    That should be a function. Commented May 15, 2022 at 19:03
  • 1
    Did you really mean to send output to a file named "2"? If you're trying to send output to stderr, use 1>&2 (or just >&2) rather than 1>2. Also, there are a bunch of things in there that really should be double-quoted to avoid parsing weirdness. Finally, I'd recommend stopping on end-of-file as well as/instead of when getting "quit" (you could use while read line && [[ $line != "quit" ]]; do ... to check for either). Commented May 15, 2022 at 20:46

1 Answer 1

2

You have several options (and I'm repeating the script from the question, not fixing any other errors (?) like redirecting to a file with name 2):

  1. Have the alias define a function and execute it immediately

    alias longest='f() { data=""; len=0; line=""; while [[ $line != "quit" ]]; do read line; [[ $line != "quit" ]] && [[ ${#line} -gt len ]] && len=${#line} data=$line; done; echo $len; echo $data 1>2; }; f'
    
  2. Create a script and save it in a directory from your PATH, usually ~/bin:

    File ~/bin/longest:

    #!/bin/bash
    data="";
    len=0;
    line="";
    while [[ $line != "quit" ]]; do
       read line;
       [[ $line != "quit" ]] && [[ ${#line} -gt len ]] && len=${#line} data=$line;
    done;
    echo $len;
    echo $data 1>2
    

    and finally chmod +x ~/bin/longest.

  3. Define the function in your .bashrc file and then call it on demand.

  4. Avoid the complicated, home-grown code and go for something much simpler. The behavior and output will not be identical, but should be sufficient.

    alias longest="awk '{print length, \$0}' | sort -nr | head -1"
    
Sign up to request clarification or add additional context in comments.

2 Comments

Why wrap a function in an alias instead of just defining & using the function it directly? Wrapping it in an alias just seems like useless complexity (& opportunity for parsing confusion). Speaking of which, in the awk version, you need to escape the $ to keep $0 from being expanded (since it's in double-quotes as the alias is being defined).
@GordonDavisson thanks for the hint, I have fixed the awk example. As for why a function in an alias? Because the question was asking for a way to do this with an alias. I provided other options to suggest different solutions that do not necessarily involve a (complicated) alias.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.