How to define variable in Jenkins declarative pipeline? I defined variable in declarative Jenkins pipeline script but having issues with simple variable declaration.

Here is my script:

pipeline {
    agent none
    stages {
        stage("first") {
            def foo = "foo" // fails with "WorkflowScript: 5: Expected a step @ line 5, column 13."
            sh "echo ${foo}"
        }
    }
}


but it's shows error:

org.codehaus.groovy.control.MultipleCompilationErrorsException:
startup failed:
WorkflowScript: 5: Expected a step @ line 5, column 13.
    def foo = "foo"
    ^
The variable must be defined in a script section.

pipeline {
    agent none
    stages {
        stage("first") {
            script {
                 foo = "bar"
            }
            sh "echo ${foo}"
        }
    }
}