all. How can I write all sessions into an array? (screen -ls) i.e:

23257.name_session1 (06/25/2017 10:41:18 PM)(Detached)

22011.name_session2 (06/25/2017 07:50:33 PM)(Detached)

arr[0] = 23257.name_session1;

arr[1] = 22011.name_session2;

example commands: screen -ls | grep -oP '\d+.\S+' | grep -oP '(?<=\d.)\S+'

arr=($(screen -ls | egrep -o '\t.+\t' | sort -n))


arr=($(...)): Assigns a series of whitespace-delimited values to an indexed array, e.g. arr=($(1 2 three 4)) defines $arr[0]=1, $arr[1]=2, $arr[2]=three, and $arr[3]=4.

screen -ls: The base function whose output is to be filtered. This outputs the following (example) lines:

There are screens on:
    79481.ttys000.macbook   (Detached)
    80513.ttys004.macbook   (Attached)
    79513.ttys002.macbook   (Detached)
3 Sockets in /var/folders/ht/0ty_c8mx16v69csh949g3rvr0000gn/T/.screen.

Note that the white-space on the lines containing the session details are tab-delimited columns.

egrep -o '\t.+\t': Selects only the output on each line that falls between two tab characters (includes the tabs). This isolates the pid.tty.host strings.

sort -n: Sort each line by numerical order, from smallest pid to greatest pid.

The resulting array contains the pid.tty.host identifiers for each screen session (both attached and detached), with $arr[0] containing the session that started first, through to $arr[n-1] containing the nth session, started most recently.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.