Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>So, I wrote a shell script in a file named test</p> </blockquote> <pre><code>$ sh test hello </code></pre> <p>This isn't a <code>zsh</code> script: you're calling it with <code>sh</code>, <a href="http://linux.die.net/man/1/sh" rel="noreferrer">which is (almost certainly) <code>bash</code></a>. If you've got the shebang (<code>#!/bin/zsh</code>), you can make it executable (<code>chmod +x &lt;script&gt;</code>) and run it: <code>./script</code>. Alternatively, you can run it with <code>zsh &lt;script&gt;</code>. </p> <blockquote> <p>the result fails. What's wrong with the code?</p> </blockquote> <p>You can wrap in braces: </p> <pre><code>echo ${1} # This'll work with or without the braces. echo ${1[3,5]} # This works in the braces. echo $1[3,5] # This doesn't work. </code></pre> <p>Running this: <code>./test-script hello</code> gives:</p> <pre><code>./test-script.zsh hello hello llo ./test-script.zsh:5: no matches found: hello[3,5] </code></pre> <blockquote> <p>Also I don't know how to extract subString from n to the last. Perhaps do I have to use Array split by regex?</p> </blockquote> <p>Use the <code>[n,last]</code> notation, but wrap in braces. We can determine how long our variable is with, then use the length:</p> <pre><code># Store the length of $1 in LENGTH. LENGTH=${#1} echo ${1[2,${LENGTH}]} # Display from `2` to `LENGTH`. </code></pre> <p>This'll produce <code>ello</code> (prints from the 2nd to the last character of <code>hello</code>). </p> <p>Script to play with:</p> <pre><code>#!/usr/local/bin/zsh echo ${1} # Print the input echo ${1[3,5]} # Print from 3rd-&gt;5th characters of input LENGTH=${#1} echo ${1[2,${LENGTH}]} # Print from 2nd -&gt; last characters of input. </code></pre> <blockquote> <p>You can use the cut command:</p> </blockquote> <p>But that would be using extra baggage - <code>zsh</code> is quite capable of doing all this on it's own without spawning multiple sub-shells for simplistic operations. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload